ECMAScript 对象扩展 assgin

58 阅读1分钟
 // 将源对象赋值到目标对象,如果目标对象与源对象中,有相同的值, 源对象覆盖目标对象

 const source1 = {
   a: 123,
   b: 123
 }
 const source2 = {
   b:789,
   d:789
 }
 const target = {
   a:456,
   c:456
 }
//第一个对象为目标对象, 后面对象,覆盖第一个对象
 const result = Object.assign(target,source1,source2)
 console.log(target)
 console.log(result === target)

// 用法:复制一个全新的对象
function func(obj){
  const funcObj = Object.assign({},obj)
  return funcObj
}
const obj = {name: 'global obj'}
func(obj)
console.log(obj)