8 common JavaScript object methods

Posted by justice1 on Sat, 11 Sep 2021 20:23:21 +0200

1.Object.assign()
Function: copy all enumerable self attributes from one or more source objects to the target object.
Syntax: Object.assign(target,... sources)
Parameters:
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);
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// 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.
2.Object.values()
Function: returns an array of enumerable attribute values of a given object in the same order as that provided by the for... In loop.
Syntax: Object.values(obj)
Parameters:
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
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]

3.Object.prototype.hasOwnProperty()
Function: returns a Boolean value indicating whether the object has the specified attribute as its own attribute. 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)
Parameters:
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;
console.log(object1.hasOwnProperty('property1'));// true
console.log(object1.hasOwnProperty('toString'));// false
console.log(object1.hasOwnProperty('hasOwnProperty'));// false

4.Object.keys()
Function: returns an array of enumerable attribute names of a given object, iterating in the same order as the normal loop.
Syntax: Object.keys(obj)
Parameters:
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
};
console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]

5.Object.prototype.toString()
Function: 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) {
  this.name = name;
}
const dog1 = new Dog('Gabby');
Dog.prototype.toString = function dogToString() {
  return `${this.name}`;
};
console.log(dog1.toString());
// 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.
6.Object.freeze()
Function: 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)
Parameters:
obj: object to freeze.
Return value: the object passed to the function.

const obj = {
 prop: 42
};
Object.freeze(obj);
obj.prop = 33;
// Throws an error in strict mode
console.log(obj.prop);
// expected output: 42

7.Object.entries()
Function: returns an array of enumerable string key attribute [key, value] pairs of a given object. It is similar to iterating with a for... In loop, except that the for... In 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)
Parameters:
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"

8.Object.is()
Function: a method to judge whether two values are the same.
Syntax: Object.is(value1, value2);
Parameters:
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 '==='
Object.is(25, 25);                // true
Object.is('foo', 'bar');          // false
Object.is(foo, foo);              // true
// Case 2: Signed zero
Object.is(0, -0);                 // false
Object.is(0n, -0n);               // true
// Case 3: NaN
Object.is(NaN, 0/0);              // true
Object.is(NaN, Number.NaN)        // true

Topics: Javascript