1. 程式人生 > >淺談Object.assign()

淺談Object.assign()

scrip 目標 rip asc bject sign log code man

Object.assign()方法用於將所有可枚舉屬性的值從一個或多個源對象復制到目標對象。返回值為目標對象。

const obj = {
  name: "zs",
  age: 18,
  sex: "man"
};
const newObj = Object.assign({ newname: "lisi" }, obj);
console.log(newObj, obj);
// { newname: ‘lisi‘, name: ‘zs‘, age: 18, sex: ‘man‘ } { name: ‘zs‘, age: 18, sex: ‘man‘ }

淺談Object.assign()