Method 1
Copy codeThe Code is as follows:
Function clone (obj ){
Var o;
Switch (typeof obj ){
Case 'undefined': break;
Case 'string': o = obj + ''; break;
Case 'number': o = obj-0; break;
Case 'boolean': o = obj; break;
Case 'object ':
If (obj = null ){
O = null;
} Else {
If (obj instanceof Array ){
O = [];
For (var I = 0, len = obj. length; I <len; I ++ ){
O. push (clone (obj [I]);
}
} Else {
O = {};
For (var k in obj ){
O [k] = clone (obj [k]);
}
}
}
Break;
Default:
O = obj; break;
}
Return o;
}
Method 2
Copy codeThe Code is as follows:
Function clone2 (obj ){
Var o, obj;
If (obj. constructor = Object ){
O = new obj. constructor ();
} Else {
O = new obj. constructor (obj. valueOf ());
}
For (var key in obj ){
If (o [key]! = Obj [key]) {
If (typeof (obj [key]) = 'object '){
O [key] = clone2 (obj [key]);
} Else {
O [key] = obj [key];
}
}
}
O. toString = obj. toString;
O. valueOf = obj. valueOf;
Return o;
}
Method 3
Copy codeThe Code is as follows:
Function clone3 (obj ){
Function Clone (){}
Clone. prototype = obj;
Var o = new Clone ();
For (var a in o ){
If (typeof o [a] = "object "){
O [a] = clone3 (o [a]);
}
}
Return o;
}