- Method JSON
var obj2 = JSON.parse (JSON.stringify (obj1 )) // deep copy Recursively (to itself) method for determining the type attribute of the first layer, the properties of the multilayer copy target
var OBJ1 = {
name: 'ZS',
Age: 18 is,
Dog: {
name: 'Golden',
Age: 2
},
Friends : [ 'WW', 'Lil']
}
var obj2 = {};
function the deepCopy (O1, O2) {
for (var K in O1) {
// if the property in the first layer is an array of complex type
if (o1 [ K] the instanceof the array) {
o2 [k] = []; // if o1 [k] is an array, then to the o2 [k] is also prepared a copied content storage array
deepCopy (o1 [k], o2 [k ]);
}
// if the properties in the first layer is a complex type object
the else IF (O1 [K] the instanceof Object) {
O2 [K] = {};
the deepCopy (O1 [K], O2 [K]);
} {the else
O2 [K] O1 = [K];
}
}
}
the deepCopy (OBJ1, obj2);
obj1.name = 'Lisi';
obj1.dog.name = 'rhubarb';
the console.log (OBJ1);
the console.log (obj2);Object.created ();
// the Object.create () specific use
var obj = {name: 'Lisi'};
var = newobj the Object.create (obj);
// newobj prototype ( proto ) pointing obj, comprising the obj properties and methods
Deep copy of three ways
Guess you like
Origin www.cnblogs.com/zcsmile/p/10969287.html
Recommended
Ranking