1. Shallow copy:
If it is a value type, copy the value. If it is a reference type, copy the reference address.
Object. Prototype. shallowclone = function (){
VaR OBJ = new object;
For (VAR property in this ){
OBJ [property] = This [property];
}
Return OBJ;
}
Ii. Deep copy:
Both the value type and reference type are recopied.
Function deepclone (object ){
If (Object = NULL) return NULL;
If ("number Boolean string undefined"). indexof (typeof object). tolowercase ()> = 0)
Return object; // return directly for the Value Type
If (Object instanceof array ){
VaR newarray = [];
For (VAR I = 0; I <object. length; I ++ ){
Newarray. Push (deepclone (object [I]);
}
Return newarray;
}
VaR OBJ = new object ();
For (VAR property in object ){
OBJ [property] = deepclone (object [property]);
}
Return OBJ;
}
Iii. test:
Click here to download the test code