yangxuanLL

ES6中Object.assign()

Object.assign()

语法:Object.assing(target,…sources)

一、合并对象

const first = { name: "Bob" };
const last = { lastName: "Smith" };

let person = Object.assign(first, last);
console.log(person);/*{ name: 'Bob', lastName: 'Smith' }*/

二、克隆对象

const obj = { person: "Bob Smith"};
const clone = Object.assign({}, obj);
console.log(obj);/*{ person: 'Bob Smith' }*/

三、"nullundefined 源被视为空对象一样对待,不会对目标对象产生任何影响。"

const test = null;
const test1 = Object.assign({},test);
console.log(test1);/*{}*/

const test2=undefined;
const test4=Object.assign({},test2);
console.log(test4);/*{}*/

通过以上可以看出,test1和test4依然空对象

posted on 2019-06-24 11:32  yangxuanLL  阅读(629)  评论(0编辑  收藏  举报

导航