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. For example:
If you modify the value of B, then the value of the A variable is also changed.
Cloning or copying is divided into 2 kinds: Shallow clone (copy), deep clone (copy);
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.
My code is as follows: The main use of recursion.
function Cloneobject (obj) {
if (obj = = = null) {
return null;
}else if (obj instanceof Array) {
var arr = [];
for (var i = 0, Ilen = obj.length; i < Ilen; i+=1) {
Arr[i] = Obj[i];
}
return arr;
}else if (obj instanceof Date | | obj instanceof REGEXP | | obj instanceof Function) {
return obj;
}else if (obj instanceof Object) {
var o = {};
for (var i in obj) {
if (Obj.hasownproperty (i)) {
O[i] = Cloneobject (Obj[i]);
}
}
return o;
}else{
return obj;
}
}
Deep cloning of JavaScript