十六、对象合并
题目:用尽可能多的方式合并两个对象
const a = {a:1, b:4}
const b = {b:2, c:3}
- 使用Object.assign
let obj = Object.assign(a,b)
console.log(obj) //{a:1, b:2, c:3} -- b被覆盖
- ES6新方式…
let obj = {...a, ...b}
console.log(obj) //{a:1, b:2, c:3} -- b被覆盖
- 手动封装
function joinObj(target, source){
for (var k in source) {
target[k] = source[k]
}
return target
}
console.log(joinObj(a,b)) //{a:1, b:2, c:3} -- b被覆盖