Object.assign()

getOwnPropertySymbols

  

  The Object.assign() method only copies enumerable and own properties from a source object to a target object.

  Note that Object.assign() does not throw on null or undefined source values.

  

function test() {
  'use strict';

  let a = { b: {c: 4} , d: { e: {f: 1} } };
  let g = Object.assign({}, a);
  let h = JSON.parse(JSON.stringify(a));
  console.log(JSON.stringify(g.d)); // { e: { f: 1 } }
  g.d.e = 32;
  console.log('g.d.e set to 32.'); // g.d.e set to 32.
  console.log(JSON.stringify(g)); // { b: { c: 4 }, d: { e: 32 } }
  console.log(JSON.stringify(a)); // { b: { c: 4 }, d: { e: 32 } }
  console.log(JSON.stringify(h)); // { b: { c: 4 }, d: { e: { f: 1 } } }
  h.d.e = 54;
  console.log('h.d.e set to 54.'); // h.d.e set to 54.
  console.log(JSON.stringify(g)); // { b: { c: 4 }, d: { e: 32 } }
  console.log(JSON.stringify(a)); // { b: { c: 4 }, d: { e: 32 } }
  console.log(JSON.stringify(h)); // { b: { c: 4 }, d: { e: 54 } }
}

test();

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

原文地址:https://www.cnblogs.com/tekkaman/p/6622127.html