Home  >  Article  >  Web Front-end  >  What are the methods of object in es6? Test test test

What are the methods of object in es6? Test test test

王林
王林Original
2022-10-20 14:48:561086browse

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

New methods of Object in ES6

Object.is()

Object.is() It is used to compare whether two values ​​are strictly equal, and is used with strict comparison operators The behavior of ( === ) is basically the same, but there are two new differences based on the third equal sign judgment.

Object.is() has only two differences: one is that 0 is not equal to -0, and the other is that NaN is equal to itself.

Syntax: Object.is(value1, value2);

Pass in two values ​​to be compared to determine whether they are the same. If they are congruent, it will return true, if they are not congruent, it will return false.

The third equal sign is used to determine whether the types of values ​​are equal and whether the reference addresses are equal. So Object.is() also judges that the data of reference types such as objects and arrays are not equal.

Object.assign()

The Object.assign() method is used to merge objects, copying all enumerable properties of the source object (source) to the target object (target).

Note:

This is a shallow copy! !

If the target object and the source object have attributes with the same name, or multiple source objects have attributes with the same name, the later attributes will overwrite the previous attributes! !

let obj1 = {a:{b:1},b:22};
let obj2 = Object.assign({b:11},obj1);
console.log(obj2 ); // {b:22,a:{b:1}}
//Modify the attribute value of obj1
obj1.a.b = 2;
//This will affect the attribute value of obj2 , this is caused by shallow copy
console.log(obj2.a.b); // 2

Object.keys(), Object.values(), Object.entries()

Object.keys() method returns an array whose members are the key arrays of all traversable (enumerable) properties of the parameter object itself (excluding inherited ones).

The Object.values() method returns an array whose members are the key value arrays of all traversable (enumerable) properties of the parameter object itself (excluding inherited ones).

The Object.entries() method returns an array whose members are key-value arrays of all traversable (enumerable) properties of the parameter object itself (excluding inherited ones).

let obj = {name:"winne",age:22};
let objKeys = Object.keys(obj);
let objValues ​​= Object.values(obj);
let objItem = Object.entries(obj);

console.log(objKeys); //["name","age"]
console.log(objValues); //["winne" ,22]
console.log(objItem); //[["name","winne"],["age",22]]

Take out the desired part, We can then traverse the array to obtain each item.

//for..of traversal

for( let key of objKeys){
console.log(key)
}
//name
/ /age

//forEach traversal
objKeys.forEach(function(item,index){
console.log(item,index);
})
//name 0
//age 1

[Related recommendations: javascript video tutorials, programming videos]

The above is the detailed content of What are the methods of object in es6? Test test test. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn