神奇的Object.assign()(Magic object. Assign())

Object.assign() 方法用于将所有可枚举的属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。

1、Object.assign()可以在对象为一层的时候,实现简单的“深拷贝功能”

2、Object.assign()可以在对象为两层或者多层的时候,此时第一种的“深拷贝功能”便无法继续使用。

var obj1 = {name:{a:’1′,b:’2′}}var obj2 = Object.assign({}, obj1 )obj2 .name.a = ‘4’console.log(obj1 )   //name: {a: 4, b: ‘2’}

console.log(obj2 )   //name: {a: 4, b: ‘2’}

————————

The < strong > object. Assign() method is used to copy the values of all enumerable attributes from one or more source objects to the target object. It will return the target object

1. < strong > object. Assign() can implement a simple “deep copy” function when the object is the first layer < / strong >

2. < strong > object. Assign() can be used when the object is two or more layers. At this time, the first “deep copy function” can no longer be used

var obj1 = {name:{a:’1′,b:’2′}}var obj2 = Object.assign({}, obj1 )obj2 .name.a = ‘4’console.log(obj1 )   //name: {a: 4, b: ‘2’}

console.log(obj2  )   // name:   {a:   4,   b:  ‘ 2’}