JS generally has two values of different data types:
Basic types (including Undefined,null,boolean,string,number), passed by value;
Reference types (including arrays, objects) are passed by address, and reference types are in-memory addresses when values are passed.
Clones or copies are divided into 2 types:
Shallow clone: The base type is a value pass, and the object is still passed as a reference.
Deep cloning: All elements or attributes are completely cloned and completely independent of the original reference type, that is, the original object will not be modified when the object's properties are modified later.
The code is as follows:
function Cloneobject (obj) {
var o = Obj.constructor = = = Array? [] : {};
for (var i in obj) {
if (Obj.hasownproperty (i)) {
O[i] = typeof obj[i] = = = "Object"? Cloneobject (Obj[i]): Obj[i];
}
}
return o;
}
Another: If it is a simple array, the element does not have the value of the reference type, can be directly used Array.concat (), or Array.slice (0), to deep copy an array, so simple and efficient. The array's concat () and slice () would have generated a new array, and the original array would not have been affected. Note, however, that you want to make sure that the elements in the copied array do not have a value for the reference type.
This is another method of deep cloning, very simple, very practical:
The code is as follows:
var s = json.stringify (obj);
var o = json.parse (s);
The Clone pee in JS