Object.assign () method Detailed

Object.assign () method is used to enumerate all the attribute values ​​are copied from one or more source object to the target object. It will return to the target object.

grammar

Object.assign(target, ...sources)

parameter:

target

target

source

Source object

 

return value

target

Description:

If the target object property with the same key, the attributes are covered by the source object property. Properties of the source object will similarly cover behind the front of the properties of the source object.

String type and Symbol types of properties will be copied.

In the case of an error, for example, if the property can not write, can cause TypeError, if you add a property before any raises an error, you can change the target object.

Note that, Object.assign skip value is null or not the source object is undefined.

Example:

1. The return value is the target object, the return value is the target object, the return value is the target audience!

var obj1 = {
    name: 'jack',
    age: 25
}

var obj2 = {
    name: 'curry'
}
var obj3 = {
    age: 30
}

Object.assign(obj1, obj2, obj3) ;

// { name: 'curry', age: 30 }
console.log(obj1) ;

Thus, Object.assign () is a target object to be modified.

2. Copy Objects

var obj1 = {
    a: 1,
    b: 2,
    c: 'usa'
}
targetObj = {} ;
var target = Object.assign(targetObj, obj1) ;

// { a: 1, b: 2, c: 'usa' }
console.log(targetObj)
// true
console.log(targetObj === target) ;

Thus, Object.assign () return is indeed the target object (targetObj).

Note that, Object.assign () to copy the object, the first level deep copy attribute after shallow copy level attributes.

In other words, the source object if a property value of the property is a reference, then just copy this reference.

var obj1 = {
    a: 1,
    b: 2,
    c: 'usa'
}

var targetObj = Object.assign({}, obj1) ;
// { a: 1, b: 2, c: 'usa' }
console.log(targetObj)

obj1.a = 4 ;
console.log('--------------------') ;
// { a: 1, b: 2, c: 'usa' }
console.log(targetObj)
// { a: 4, b: 2, c: 'usa' }
console.log(obj1)

In this case, the source object property value is the basic data type (Number, String), where the target object belongs to the copy attribute value deep copy.

var obj1 = {
    name: {first: 'Stenphen', second: 'Jack'}
}

var targetObj = Object.assign({}, obj1) ;
// { name: { first: 'Stenphen', second: 'Jack' } }
console.log(targetObj)

obj1.name.first = 'qinney' ;
console.log('--------------')
// { name: { first: 'qinney', second: 'Jack' } }
console.log(obj1)
// { name: { first: 'qinney', second: 'Jack' } }
console.log(targetObj)

Attribute value of the source object is a reference type, the only copy of the target object reference, are shallow copy.

3. Merge objects

var obj1 = {a: 1} ;
var obj2 = {b: 2} ;
var obj3 = {c: 3} ;

var targetObj = Object.assign({}, obj1, obj2, obj3) ;
// { a: 1, b: 2, c: 3 }
console.log(targetObj)

4. The object has the same properties combined

var obj1 = {a: 1, b: 6, c: 100} ;
var obj2 = {b: 2} ;
var obj3 = {c: 5} ;

var targetObj = Object.assign({}, obj1, obj2, obj3) ;
// { a: 1, b: 2, c: 5 }
console.log(targetObj)

The same attribute, the attribute value will be covered later.

Guess you like

Origin www.cnblogs.com/qinney1109/p/11200347.html