Object.assign simply copies the enumerable properties of the Object. If the property of the object is an object. Object.assign cannot copy this Object over. Just copy that address over. So Object.assign is not a deep clone.
- Example:
const log = console.log;
function test () {
'use strict';
let obj1 = {a: 0, b: {c: 0}}
let obj2 = Object.assign({}, obj1);
log(json.stringify (obj2)) // Copy the object to another object. obj1.a = 1;log(JSON.stringify(obj1)); // The value is changedlog(JSON.stringify(obj2)); // But does not change the copied object. obj2.a = 3;log(json.stringify (obj1)) // Changing 2 does not change 1log(json.stringify (obj2)) // Change 2 only to change obj2.b.c = 344log(json.stringify (obj1)) // Change the value of an object in 2. The inside of 1 will change.log(json.stringify (obj2)) // change 2 // Deepclone
obj1 = { a: 0 , b: { c: 0}};
letobj3 = JSON.parse(JSON.stringify(obj1)); // Clone obj1.a = 4 // change 1 obj1.b.c = 4 console.log(json.stringify (obj3)) // Unchanged 2}test(a)Copy the code