This is the 26th day of my participation in the August Text Challenge.More challenges in August
Es6 has some new properties for Object compared to previous ones. This article illustrates several attributes
Object.assign()
Basic usage
The object.assign () method is used to merge objects. It copies all the enumerable properties of the source Object to the target Object.
const target = { a: 1 };
const source1 = { b: 2 };
const source2 = { c: 3 };
Object.assign(target, source1, source2);
target // {a:1, b:2, c:3}
Copy the code
The object.assign () method takes the first argument to the target Object and the rest to the source Object.
Note that if the target object has an attribute with the same name as the source object, or if multiple source objects have an attribute with the same name, the following attribute overrides the preceding one.
If there is only one argument, object.assign () returns that argument directly.
If the parameter is not an object, it is converted to an object and then returned.
Because undefined and NULL cannot be converted to objects, they are reported as arguments.
If non-object arguments occur at the location of the source object (that is, non-first arguments), the rules are different. First, these parameters are converted to objects, and if they cannot, they are skipped. This means that if undefined and null are not the first arguments, no error is reported.
Object.is()
ES5 compares whether two values are equal with only two operators: the equality operator (==) and the strict equality operator (===). They both have the disadvantages that the former automatically converts the data type, the latter that NaN is not equal to itself, and +0 equals -0. JavaScript lacks the operation that, in all environments, two values should be equal as long as they are the same.
ES6 proposes the “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, basically the same behavior as the strict comparison operator (===).
Object.is('foo'.'foo')
// true
Object.is({}, {})
// false
Copy the code
Object.getOwnPropertyDescriptors()
ES5 Object. GetOwnPropertyDescriptor () method returns an Object attribute description of Object (descriptor). ES2017 introduced Object. GetOwnPropertyDescriptors () method, which returns the specified all its attributes (not inherit property) a description of the Object.
const obj = {
foo: 123.get bar() { return 'abc'}};Object.getOwnPropertyDescriptors(obj)
// { foo:
// { value: 123,
// writable: true,
// enumerable: true,
// configurable: true },
// bar:
// { get: [Function: get bar],
// set: undefined,
// enumerable: true,
// configurable: true } }
Copy the code
The above code, the Object. GetOwnPropertyDescriptors () method returns an Object, all of the original Object attribute names are attributes of the Object, the description of the corresponding attribute value is the attribute Object.
__proto__ properties
Object inheritance in the JavaScript language is implemented through prototype chains. ES6 provides more ways to manipulate prototype objects.
__proto__ properties
The __proto__ property (two underscores before and two underscores behind) reads or sets the prototype of the current object. This property is currently deployed in all browsers, including Internet Explorer 11.
Const obj = {method: function() {// const obj = {method: function() {... }}; obj.__proto__ = someOtherObj; Var obj = object.create (someOtherObj); obj.method = function() { ... };Copy the code
This attribute is not written in the body of ES6, but in the appendix, because the double underline around __proto__ indicates that it is essentially an internal attribute, not a formal external API, and was only added to ES6 due to widespread browser support. The standard clearly states that only browsers must deploy this property, not other runtime environments, and that new code is better off assuming this property does not exist. So don’t use this property for semantic reasons, or for compatibility reasons, Instead, use the following object.setPrototypeof () (write operation), Object.getPrototypeof () (read operation), object.create () (generate operation).
Object.values()
The Object.values method returns an array of all the key values of an Enumerable property of the parameter Object itself (not including inheritance).
const obj = { foo: 'bar', baz: 42 };
Object.values(obj)
// ["bar", 42]
Copy the code
Returns the order of the members of an array, as described in the “Traversal of properties” section of this chapter.
const obj = { 100: 'a', 2: 'b', 7: 'c' };
Object.values(obj)
// ["b", "c", "a"]
Copy the code
In the above code, the property named value is traversed in ascending order of value, so the return order is B, C, and A. Object.values returns only traversable properties of the Object itself.