JS generally has two different data types of values:
Basic types (including Undefined,null,boolean,string,number), passed by value;
Reference types (including arrays, objects), delivered by address, and reference types are in-memory addresses when the value is passed.
Clones or copies are divided into 2 types:
Shallow cloning: The base type is passed by value 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 is not modified when the object's properties are modified later.
Copy Code code 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 a simple array, there is no reference type value in the element, you can directly use Array.concat (), or Array.slice (0), to deep copy an array, so simple and efficient. The concat () and slice () of the array 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 reference type value.
This is another method of deep cloning, very simple, very practical:
Copy Code code as follows:
var s = json.stringify (obj);
var o = json.parse (s);