8 common JavaScript object methods

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

Function: 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.
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)
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]

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)
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

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)
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"]

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}`;
// 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.
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)
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

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)
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"

Function: a method to judge whether two values are the same.
Syntax: Object.is(value1, 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 '==='
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