Object.assign()用于将所有可枚举属性的值从一个或多个源对象复制到目标对象
const target = { a: 1, b: 2 }; const source = { b: 4, c: 5 }; const returnedTarget = Object.assign(target, source) console.log(target); //{ a: 1, b: 4, c: 5 } console.log(source); //{ b: 4, c: 5 } console.log(returnedTarget); //{ a: 1, b: 4, c: 5 }
const target = { a: 1, b: 2 }; const returnedTarget = Object.assign({}, source) returnedTarget.a1 = 'a1' console.log(target); //{ a: 1, b: 2 } console.log(returnedTarget); //{ a: 1, b: 2, a1: "a1" }