实现两个对象部分属性的合并,比如说t,s两个对象,当s中有t不存在的属性时给t加上s的属性并赋值。要求不更改t的引用地址。方法如下
const merge = (target, source)=> {
for (const key in source) {
if (typeof target[key] === 'undefined') {
target[key] = source[key];
}
}
return target;
}
补充1个测试案例
const a = { name: undefined, age: 2 };
const b = { name: '小明', weigth: 100 };
const c = merge(a,b)
console.log(a) // { name: '小明', age: 2,weigth: 100 }
console.log(c) // { name: '小明', age: 2,weigth: 100 }