A New Method of Object Creation
ES6 allows direct writing of variables and functions as attributes and methods of objects. Such writing is more concise.
The above code shows that ES6 allows you to write only attribute names and not attribute values in objects. In this case, the attribute value is equal to the variable represented by the attribute name.
//Example 1:
var foo = 'bar';
var baz1 = {foo};
console.log(baz1);// Object {foo: "bar"};
var baz2 = {foo:'bar2'};
console.log(baz2)// Object {foo: "bar2"}
//Example 2:
function f1(x,y) {
return {x,y} ;
}
function f2(x,y) {
return {x:x,y:y};
}
console.log(f1(1,2));// Object {x: 1, y: 2}
console.log(f2(1,2));// Object {x: 1, y: 2}
//Example 3:
//The following two objects are identical
var o1 = {
method() {//ES6 can write a function directly like this
return 'hello!';
}
}
var o2 = {
method: function () {
return 'hello!'
}
}
console.log(o1.method());//hello
console.log(o2.method());//hello
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
Object.is()
ES5 compares whether two values are equal, with only two operators: the equality operator (==) and the strict equality operator (===). They all have shortcomings, the former automatically converts data types, the latter NaN is not equal to itself, and + 0 is equal to - 0.
JavaScript lacks an operation, and in all environments, as long as the two values are the same, they should be equal.
ES6 proposes a "Same-value equality" algorithm to solve this problem. Object.is is a new way to deploy this algorithm. It is used to compare whether two values are strictly equal, which is basically consistent with the behavior of the strict comparison operator (===).
//Example:
console.log(+0 === -0);//true
console.log(NaN === NaN);//false
console.log(Object.is(+0, -0));//false
console.log(Object.is(NaN,NaN));//true
- 1
- 2
- 3
- 4
- 5
Object.assign(); object copy (shallow copy)
//example
var target = {a:1};
var source1 = {b:2};
var source2 = {c:3};
Object.assign(target,source1,source2);
console.log(target);//Object {a: 1, b: 2, c: 3}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Note: For such nested objects, once they encounter the same name attribute, Object.assign is handled by replacing, not adding.
//example
var target = { a: { b: 'c', d: 'e' } };
var source = { a: { b: 'hello' } };
Object.assign(target, source);
console.log(target);// { a: { b: 'hello' } };
- 1
- 2
- 3
- 4
- 5
Objects traverse Object.keys Object.values Object.entries
//ES5 introduces the Object.keys method and returns an array whose members are keynames of all enumerable attributes of the parameter object itself (without inheritance).
//The Object.values method returns an array whose members are the key values of all enumerable attributes of the parameter object itself (excluding inheritance).
//The Object.entries method returns an array of key pairs whose members are all enumerable attributes of the parameter object itself (without inheritance).
var obj = { foo: "bar", baz: 42 };
Object.keys(obj)
// ["foo", "baz"]
var obj = { foo: "bar", baz: 42 };
Object.values(obj)
// ["bar", 42]
var obj = { foo: 'bar', baz: 42 };
Object.entries(obj)
// [["foo", "bar", ["baz", 42]] Two-dimensional arrays
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
Object.setPrototypeOf(),Object.getPrototypeOf()
The u proto u attribute (two underscores, front and back) is used to read or set the prototype object of the current object
This attribute is not written in the body of ES6, but in the appendix. The reason is that the double underlines around u proto indicate that it is essentially an internal attribute, not a formal external API. Only because of the browser's extensive support, it is added to ES6. The standard clearly stipulates that only browsers must deploy this property, other running environments do not necessarily need to deploy, and the new code is best to think that this property does not exist. Therefore, neither semantically nor compatibly, use this attribute, but instead use Object.setPrototypeOf() (write operation), Object.getPrototypeOf() (read operation), Object.create() (generate operation) below.
//Example:
var obj = {
a:1
}
var obj2 = {
b:2
}
obj1.__proto__ = obj2;
console.log(obj1.b);// 2
//Equivalent to the following method.
Object.setPrototypeOf(obj1, obj2);
console.log(obj1.b);// 2
console.log(Object.getPrototypeOf(obj1));//Object b
This article is reproduced from:
C_Kite
https://blog.csdn.net/c_kite/article/details/72598904- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17