Usage of Object.assign()

This method is used to assign all enumerable attribute values ​​from one or more source objects (sources) to the target object (target), and return the target object.

Object.assign(target, ...sources)
One, the copy of the object
const target = {
    
     a: 1, b: 2 };
const source1 = {
    
     b: 4, c: 5 };
const source2 = {
    
     b: 6, c: 7 };
const obj = Object.assign(target,source1,source2);
console.log(obj); // (a: 1, b: 6, c: 7)

Note:
1. The attributes of the source object and the target object are different, they will be copied to the target object;
2. If the target object and the source object have the same attributes, the attribute value of the target object will be overwritten by the attribute value of the source object;
3. If there are multiple source objects with the same attributes, the attributes of the target object will be overwritten by the attributes of the last source object.

2. Inherited properties and non-enumerable properties cannot be copied
const obj1 = Object.create({
    
    foo: 1}, {
    
     // foo 是个继承属性。
    bar: {
    
    
        value: 2  // bar 是个不可枚举属性。
    },
    baz: {
    
    
        value: 3,
        enumerable: true  // baz 是个自身可枚举属性。
    }
});
const obj= Object.assign({
    
    }, obj1);
console.log(obj); // { baz: 3 }
//创建对象时,如果没有设置enumerable的值,默认为false(不可枚举属性),设置为true,则为可枚举属性

Note: The Object.assign method will only copy the enumerable properties of the source object itself to the target object. The inherited and non-enumerable properties will not be copied.

3. Deep copy of the object

Object.assign() copy is a shallow copy, it copies the attribute value, if the attribute value of the source object is an object obj, then the copy is the pointer of the object value (that is, the address); at this time, modify the value of obj , The target audience will be affected.
To avoid this effect, we need to make a deep copy of the object:

let obj1 = {
    
     a: 1, b: {
    
    c: 2 }};
let obj2 = {
    
     d: 2 };
let obj = Object.assign(obj2,JSON.parse(JSON.stringify(obj1)));
console.log(obj); // { d: 2, a:1, b:{ c:2 }}
obj1.b.c = 4;
console.log(obj); // { d: 2, a:1, b:{ c:2 }}
// 对象obj1.b.c 值的变化则不会再影响到目标对象 obj 的值。

Note: Deep copy can only solve the value copy of reference type, and it is still not possible to copy inherited and non-enumerable properties.

Four, abnormal will terminate the copy
const target = Object.defineProperty({
    
    }, "foo", {
    
    
    value: 1,
    writable: false
}); // target 的 foo 属性是个只读属性。

Object.assign(target, {
    
    bar: 2}, {
    
    foo2: 3, foo: 3, foo3: 3}, {
    
    baz: 4});
// TypeError: "foo" is read-only
// 注意这个异常是在拷贝第二个源对象的第二个属性时发生的。
console.log(target.bar);  // 2,说明第一个源对象拷贝成功了。
console.log(target.foo2); // 3,说明第二个源对象的第一个属性也拷贝成功了。
console.log(target.foo);  // 1,只读属性不能被覆盖,所以第二个源对象的第二个属性拷贝失败了。
console.log(target.foo3); // undefined,异常之后 assign 方法就退出了,第三个属性是不会被拷贝到的。
console.log(target.baz);  // undefined,第三个源对象更是不会被拷贝到的。
Five, the original type will be packaged into objects

The primitive type will be wrapped, null and undefined will be ignored.

const obj1 = 'aaa';
const obj2 = false;
const obj3 = true;
const obj4 = 10;
const obj= Object.assign(obj1,obj2,obj3,obj4);
console.log(obj); // { 0:'a', 1:'a', 2:'a'}

Note: Only the wrapped object of the string may have its own enumerable properties.

Guess you like

Origin blog.csdn.net/weixin_43299180/article/details/112882498