合并对象es5、6、7

方法一:循环遍历
let obj1 = {name:'a',age:18};

let obj2 = {name:'b',gender:'man'};

let obj3 = {};

for(var attr in obj1){

    obj3[attr] = obj1[attr];

}

for(var attr in obj2){

    obj3[attr] = obj2[attr];

}

console.log(obj3);//{name: "b", age: 18, gender: "man"}

方法二:assign
let obj1 = {name:'a',age:18};

let obj2 = {name:'b',gender:'man'};

let obj3 = {};

Object.assign(obj3,obj1,obj2);

console.log(obj3);//{name: "b", age: 18, gender: "man"}

方法三:ES7的对象的拓展运算和ES6的解构赋值
let obj1 = {name:'a',age:18};

let obj2 = {name:'b',gender:'man'};

let obj3 = {...obj1,...obj2};

console.log(obj3);//{name: "b", age: 18, gender: "man"}

你可能感兴趣的:(合并对象es5、6、7)