1. Object.assign ()
First look at the definition of the function:
The function argument is a target object (the object is the final return value), and the source object (here can be any number). By calling this function, you can copy all of its own property values that can be enumerated to the target object.
Object.assign (target, ... sources)
The three points we need to emphasize here are the attributes that can be enumerated:
In JavaScript, the attributes of an object are divided into enumerable and enumerable, which are determined by the value of the property. Enumerable determines whether this property can be traversed by for...in lookups.
JS Basic packaging type of prototype properties are not enumerated, such as Object, Array, number, etc., if you write such code using for...in to traverse the attributes, will not happen, no error, can continue to carry out, it seems that the jump passed directly.
If the object is not enumerable, the method for...in json.stringify () Object.keys () will fail.
Own properties:
Different from the prototype attribute, not inherited from the prototype chain, for example:
function Obj () {
This.z = 3; Own properties
}
Object inherits the attributes from the prototype.
obj.prototype.x = 1;//non-owned properties
OBJ.PROTOTYPE.Y = 2;//non-owned properties
string or symbol type, which can be assigned directly
Example one:
var O1 = {a:1};
var O2 = {B:2};
var O3 = {C:3};
var obj = object.assign (O1, O2, O3);
Console.log (obj); {a:1, b:2, c:3}
Console.log (O1); {a:1, B:2, c:3}, target object itself is changed.
This example shows that object.assign not only returns a newly spliced value, but also changes the value of the first parameter (target).
var obj = object.create ({foo:1}, {//Foo is a inherit property.
Bar: {
value:2 //bar is a non-enumerable property.
},
Baz: {
value:3,
enumerable:true
//Baz is a own enumerable property.
}
});
var copy = Object.assign ({}, obj);
Console.log (copy); {Baz:3}
In this example, because object obj is constructed using object.create (), {foo:1} is a prototype that is inherited by obj, and the second property in obj is explicitly declared enumerable, so the last copy of the previously copied property is only Baz 2. Object.create ()
Object.create (Proto [, Propertiesobject]) is a new object creation method proposed in E5, the first parameter is the prototype to inherit, if not a child function, you can pass a null, the second parameter is the object's property descriptor, This parameter is optional.
function car (desc) {
THIS.DESC = desc;
This.color = "Red";
}
Car.prototype = {
getinfo:function () {return
' A ' + this.color + ' + This.desc + '. ';
}
};
Instantiate object using the constructor function
var car = object.create (car.prototype);
Car.color = "Blue";
Alert (Car.getinfo ());
Data Property Writable: whether the configurable can be written arbitrarily: whether it can be deleted, whether it can be modified enumerable: whether it can be used for the enumeration value: value
Access property: Get (): Access Set (): Set
NEWOBJ = object.create (obj,{
t1:{
value: ' Yupeng ',
writable:true
},
bar: {
Configurable:false,
get:function () {return bar;},
set:function (value) {Bar=value}}}
)
3. Object.defineproperty ()
See here in detail