Object.assign属于浅拷贝
Object.assign只能拷贝:可被枚举的属性,自有属性,string或者Symbol类型是可以被直接分配的
var ab={ name:"没有被覆盖", age:18 } var ac = { name:"覆盖", age:24 } Object.assign(ab,ac) //如果后面的ac跟ab属于相同,它会覆盖掉前面的ab属性 console.log(ab) //Object {name: "覆盖", age: 24}
var ab={ name:"没有被覆盖", age:18 } var ac = { name:"覆盖", age:24, mel:"5489" } var ad= Object.assign({},ab,ac) //这里的{}是拷贝目标,后面的ab,ac是拷贝源,拷贝后会返回给ad console.log(ab) //Object { name:"没有被覆盖", age:18} console.log(ad) //Object {name: "覆盖", age: 24, mel: "5489"}