Sorting and sharing of Javascript object methods commonly used in java development

Javascript object methods are properties that contain built-in function definitions to effectively process and obtain information from a single object.

Syntax: ObjectName.methodName()

In this article, we will discuss the seven most commonly used object methods in Javascript, which will help you use object prototypes more easily!

1, is a method to determine whether two values are the same.

Syntax:, value2);


value1: the first value to compare.

value2: the second value to compare.

Return value: a Boolean expression indicating whether two parameters have the same value.


// Case 1: Evaluation result is the same as using '===', 25);                // true'foo', 'bar');          // false, foo);              // true
// Case 2: Signed zero, -0);                 // false, -0n);               // true
// Case 3: NaN, 0/0);              // true, Number.NaN)        // true


The Object.assign() method is used to copy all enumerable self attributes from one or more source objects to the target object.

Syntax: object. Assign (target,... Sources)


Target: target object - the object to which the source attribute is applied, which is returned after modification.

sources: source object - an object that contains the attributes you want to apply.

Return value: the modified target object.


const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
// expected output: Object { a: 1, b: 4, c: 5 }
// expected output: Object { a: 1, b: 4, c: 5 }

Note: if the attributes in the source object have the same key, the attributes in the target object will be overwritten by the attributes in the source. Properties from later sources override properties from earlier sources.


The Object.entries() method returns an array of enumerable string key attribute [key, value] pairs of the given object.

It is similar to iterating with a loop, except that the loop also enumerates the attributes in the prototype chain. The order of attributes is the same as that given by manually looping the attribute values of the object.

Syntax: Object.entries(obj)


obj: object to return its own enumerable string key attribute [key, value] pair.

Return value: an array of enumerable string key attribute [key, value] pairs of the given object.


const object1 = {name: "David", age: 23};
for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
// "name: David"
// "age: 23"


The Object.values() method returns an array of enumerable attribute values of a given object in the same order as that provided by the loop.

Syntax: Object.values(obj)


obj: the object whose enumerable attribute values are to be returned.

Return value: an array containing the enumerable property values of the given object.


const object1 = {
  a: 'somestring',
  b: 42,
  c: false
// expected output: Array ["somestring", 42, false]


The hasOwnProperty() method returns a Boolean value indicating whether the object has the specified property as its own property.

If the specified property is a direct property of the object, the method returns true - even if the value is null or undefined. Returns false if the property is inherited or not declared at all.

Syntax: hasOwnProperty(prop)


prop: String name or symbol of the attribute to be tested.

Return value: returns true if the object takes the specified attribute as its own attribute; Otherwise, it is false.


const object1 = {};
object1.property1 = 42;
// expected output: true
// expected output: false
// expected output: false


The Object.keys() method is used to return an array of enumerable attribute names of a given object, iterating in the same order as a normal loop.

Syntax: Object.keys(obj)


obj: to return an object that can enumerate its own attributes.

Return value: a string array representing all enumerable properties of a given object.


const object1 = {
  a: 'somestring',
  b: 42,
  c: false
// expected output: Array ["a", "b", "c"]

7, Object.prototype.toString()

The toString() method returns a string representing an Object. This method id is automatically called when the Object will be represented as a text value or referenced as a desired string. By default, the toString() method is inherited by each Object inherited from Object.

Syntax: toString()

Return value: a string representing the object.


function Dog(name) { = name;
const dog1 = new Dog('Gabby');
Dog.prototype.toString = function dogToString() {
  return `${}`;
// expected output: "Gabby"

Note: for Numbers and Big Ints, toString() takes the optional parameter radius, and its value must be at least 2 and at most 36.


The Object.freeze() method freezes an object, which means it can no longer be changed. Freezing an object prevents adding new properties to it, deleting existing properties, changing the enumerability, configurability, or Writeability of existing properties, and changing the values of existing properties. It also prevents its prototype from being changed.

Syntax: object. Free (obj)


obj: object to freeze.

Return value: the object passed to the function.


const obj = {
 prop: 42
obj.prop = 33;
// Throws an error in strict mode
// expected output: 42

relevant java training Develop technical knowledge, pay attention to me, and have more wonderful content to share with you!

Keywords: Java Javascript html5

Added by sunnyvatsal on Sun, 19 Sep 2021 17:02:10 +0300