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, Object.is()

Object.is() is a method to determine 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.

example:

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

2,Object.assign()

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)

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.

example:

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.

3,Object.entries()

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

example:

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

4,Object.values()

The Object.values() method 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.

example:

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]

5,Object.prototype.hasOwnProperty()

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)

parameter

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.

example:

const object1 = {};
object1.property1 = 42;
console.log(object1.hasOwnProperty('property1'));
// expected output: true
console.log(object1.hasOwnProperty('toString'));
// expected output: false
console.log(object1.hasOwnProperty('hasOwnProperty'));
// expected output: false

6,Object.keys()

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)

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.

example:

const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};
console.log(Object.keys(object1));
// 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.

example:

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.

8,Object.freeze()

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)

Parameters:

obj: object to freeze.

Return value: the object passed to the function.

example:

const obj = {
 prop: 42
};
Object.freeze(obj);
obj.prop = 33;
// Throws an error in strict mode
console.log(obj.prop);
// 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