Object literals
Shorthand notation for properties
ES6 allows object properties to be written directly as variables; the property name is the variable name, and the property value is the variable value.
Method names can also be abbreviated
If it is a Generator function, add an asterisk in front:
Property name expressions
ES6 allows expressions to be used as property names, but the expression must be placed in square brackets.
Note: The shorthand notation for properties and property name expressions cannot be used at the same time, otherwise an error will be thrown.
Object spread operator
The spread operator (...) is used to extract all enumerable properties of the parameter object and copy them to the current object.
Basic usage
Can be used to merge two objects
Notes
When custom properties have the same name as the properties in the spread operator object:If the custom property comes after the spread operator, the property with the same name inside the spread operator object will be overwritten.
If the custom property comes before the spread operator, it becomes setting default property values for the new object.
If the spread operator is followed by an empty object, it has no effect and does not throw an error.
If the spread operator is followed by null or undefined, it has no effect and does not throw an error.
New methods for objects
Object.assign(target, source_1, ···)
Used to copy all enumerable properties of the source objects to the target object.
Basic usage
- If the target object and a source object have a property with the same name, or multiple source objects have a property with the same name, the later property will overwrite the earlier one.
- If the function has only one parameter, when the parameter is an object, it returns the object directly; when the parameter is not an object, it first converts the parameter into an object and then returns it.
Because null and undefined cannot be converted to objects, an error is thrown:
Notes
assign's property copying is a shallow copy:
Replacement of properties with the same name
Handling of arrays
It treats arrays as objects, so [2,3] is first converted to {0:2,1:3}, and then property copying is performed, so property 0 of the source object overwrites property 0 of the target object.
Object.is(value1, value2)
Used to compare whether two values are strictly equal, basically similar to (===).
Basic usage
Basic usage
Differences from (===)