Object.assign()?方法用于將所有可枚舉屬性的值從一個(gè)或多個(gè)源對(duì)象復(fù)制到目標(biāo)對(duì)象铐尚。它將返回目標(biāo)對(duì)象。
let obj1 = {a:2,b:3}
let obj2 = Object.assign({},obj1);
console.log(obj1);//{a:2,b:3}
console.log(obj2);//{a:2,b:3|}
let target = {a:1,b:2};
let source ={a:2,c:3};
let result = Object.assign(target,source);
console.log(target);//{a:2,b:2,c:3}
console.log(source);//{a:2,c:2}
console.log(result);//{a:2,b:2,c:3}
let target1 = {a:1,b:2};
let source1 ={a:2,c:3};
let result1 = Object.assign(source1,target1);
console.log(target1);//{a:1,b:2}
console.log(source1);//{a:1,c:3,b:2}
console.log(result1);//{a:1,c:3,b:2}
由些可見(jiàn)笼吟,Object.assign第二個(gè)對(duì)象會(huì)覆蓋第一個(gè)對(duì)象上的值柜蜈,并改變第一個(gè)對(duì)象的值
同理,如果存在多個(gè)對(duì)象城须,也是從后往前依次覆蓋蜗细,第一個(gè)對(duì)象值被改變裆操,其余位置的對(duì)象不會(huì)改變