var obj = {a:1, arr: [};var] obj1 = obj; Shallow copy var obj2 = deepcopy (obj); Deep copy
The object created in JavaScript is a storage address, and the result of a shallow copy is that both obj and Obj1 point to the same address, and if any of the one by one elements in obj or obj1 are modified, it will affect the other
OBJ.A = 2;console.log (obj1.a); 2
A deep copy is one that copies all the properties of an object and creates a space in another area to store these properties
var myObj = {str: "myString", Num:1,myarr: [{arrgo: "I Am Go"}],obj: {innerobj: {test:25},innerstr: "myobjstring"}}// Deep copy function deepcopy (obj) {var ret, k, B;ret = (obj instanceof Array)? []: {};for (k in obj) {if ((Obj[k] instanceof Array) | | (Obj[k] instanceof Object)) {//using recursion to copy attributes from obj to ret[k] = Clone (Obj[k]);} else {Ret[k] = obj[k];}} return ret;}
Deep and shallow copies in JavaScript