One: node cloning
var p = document.getelementsbytagname ("P") [0];
var CP = P.clonenode ();//Clone P-node
var CP = P.clonenode (TRUE);//clone P node, deep clone, clone node and sub-content under node.
Two-variable cloning
① Basic types of clones
The basic type of replication, in fact, is to create a new value on the variable object, and then copy the value to be copied to the location of the new variable allocation, when the value of the variable is changed, the other value is not affected, passed by value.
(The parameters of a function can only be passed by value), which is deep cloning, and the copied object is completely independent of the new object.
② a clone of a reference type
Also copies a copy to the space allocated for the new variable, but the copied variable is a pointer to the same object stored in the heap. If you change the value of one of the variables, the other changes and is passed by the pointer.
That is, shallow cloning.
Three: Array cloning
①slice ()
1 var aarr = [0,1,2,3];
2 var m = aarr.slice (0);
3 Aarr = [3,2,1,0];
After cloning, when changing the Aarr, M will not change, realize the deep cloning of the array.
②concat ()
Implements the merging of arrays, merging the current array and an empty array.
③ the simplest way to use a loop to push () the elements in the original array into a new, empty array.
Cloning in JavaScript