Object.assign() 方法将所有可枚举(Object.propertyIsEnumerable() 返回 true)的自有(Object.hasOwnProperty() 返回 true)属性从一个或多个源对象复制到目标对象,返回修改后的对象。
介绍Object.assign()
- const obj = { a: 1 };
- const copy = Object.assign({}, obj);
- console.log(copy); // { a: 1 }
Copy to Clipboard
针对深拷贝 (en-US), 需要使用其他办法,因为 Object.assign() 只复制属性值。
假如源对象是一个对象的引用,它仅仅会复制其引用值。
- function test() {
- 'use strict';
-
- let obj1 = { a: 0 , b: { c: 0}};
- let obj2 = Object.assign({}, obj1);
- console.log(JSON.stringify(obj2)); // { "a": 0, "b": { "c": 0}}
-
- obj1.a = 1;
- console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 0}}
- console.log(JSON.stringify(obj2)); // { "a": 0, "b": { "c": 0}}
-
- obj2.a = 2;
- console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 0}}
- console.log(JSON.stringify(obj2)); // { "a": 2, "b": { "c": 0}}
-
- obj2.b.c = 3;
- console.log(JSON.stringify(obj1)); // { "a": 1, "b": { "c": 3}}
- console.log(JSON.stringify(obj2)); // { "a": 2, "b": { "c": 3}}
-
- // Deep Clone
- obj1 = { a: 0 , b: { c: 0}};
- let obj3 = JSON.parse(JSON.stringify(obj1));
- obj1.a = 4;
- obj1.b.c = 4;
- console.log(JSON.stringify(obj3)); // { "a": 0, "b": { "c": 0}}
- }
-
- test();
Copy to Clipboard
- const o1 = { a: 1 };
- const o2 = { b: 2 };
- const o3 = { c: 3 };
-
- const obj = Object.assign(o1, o2, o3);
- console.log(obj); // { a: 1, b: 2, c: 3 }
- console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
Copy to Clipboard
- const o1 = { a: 1, b: 1, c: 1 };
- const o2 = { b: 2, c: 2 };
- const o3 = { c: 3 };
-
- const obj = Object.assign({}, o1, o2, o3);
- console.log(obj); // { a: 1, b: 2, c: 3 }
Copy to Clipboard
属性会被后续参数中具有相同属性的其他对象覆盖。
- const o1 = { a: 1 };
- const o2 = { [Symbol('foo')]: 2 };
-
- const obj = Object.assign({}, o1, o2);
- console.log(obj); // { a : 1, [Symbol("foo")]: 2 } (cf. bug 1207182 on Firefox)
- Object.getOwnPropertySymbols(obj); // [Symbol(foo)]
Copy to Clipboard
- const obj = Object.create({ foo: 1 }, { // foo is on obj's prototype chain.
- bar: {
- value: 2 // bar is a non-enumerable property.
- },
- baz: {
- value: 3,
- enumerable: true // baz is an own enumerable property.
- }
- });
-
- const copy = Object.assign({}, obj);
- console.log(copy); // { baz: 3 }
Copy to Clipboard
- const v1 = 'abc';
- const v2 = true;
- const v3 = 10;
- const v4 = Symbol('foo');
-
- const obj = Object.assign({}, v1, null, v2, undefined, v3, v4);
- // Primitives will be wrapped, null and undefined will be ignored.
- // Note, only string wrappers can have own enumerable properties.
- console.log(obj); // { "0": "a", "1": "b", "2": "c" }
Copy to Clipboard
- const target = Object.defineProperty({}, 'foo', {
- value: 1,
- writable: false
- }); // target.foo is a read-only property
-
- Object.assign(target, { bar: 2 }, { foo2: 3, foo: 3, foo3: 3 }, { baz: 4 });
- // TypeError: "foo" is read-only
- // The Exception is thrown when assigning target.foo
-
- console.log(target.bar); // 2, the first source was copied successfully.
- console.log(target.foo2); // 3, the first property of the second source was copied successfully.
- console.log(target.foo); // 1, exception is thrown here.
- console.log(target.foo3); // undefined, assign method has finished, foo3 will not be copied.
- console.log(target.baz); // undefined, the third source will not be copied either.
Copy to Clipboard
- const obj = {
- foo: 1,
- get bar() {
- return 2;
- }
- };
-
- let copy = Object.assign({}, obj);
- console.log(copy);
- // { foo: 1, bar: 2 }
- // The value of copy.bar is obj.bar's getter's return value.
-
- // This is an assign function that copies full descriptors
- function completeAssign(target, ...sources) {
- sources.forEach(source => {
- let descriptors = Object.keys(source).reduce((descriptors, key) => {
- descriptors[key] = Object.getOwnPropertyDescriptor(source, key);
- return descriptors;
- }, {});
-
- // By default, Object.assign copies enumerable Symbols, too
- Object.getOwnPropertySymbols(source).forEach(sym => {
- let descriptor = Object.getOwnPropertyDescriptor(source, sym);
- if (descriptor.enumerable) {
- descriptors[sym] = descriptor;
- }
- });
- Object.defineProperties(target, descriptors);
- });
- return target;
- }
-
- copy = completeAssign({}, obj);
- console.log(copy);
- // { foo:1, get bar() { return 2 } }

JSON.stringify() | 菜鸟教程 (runoob.com)
https://www.runoob.com/json/json-stringify.htmlJSON 通常用于与服务端交换数据。