方法1
view sourceprint?01 function clone(obj){
02 var o;
03 if(typeof obj == "object"){
04 if(obj === null){
05 o = null;
06 }else{
07 if(obj instanceof Array){
08 o = [];
09 for(var i = 0, len = obj.length; i < len; i++){
10 o.push(clone(obj[i]));
11 }
12 }else{
13 o = {};
14 for(var k in obj){
15 o[k] = clone(obj[k]);
16 }
17 }
18 }
19 }else{
20 o = obj;
21 }
22 return o;
23 }
方法2
view sourceprint?01 function clone(obj){
02 var o, obj;
03 if (obj.constructor == Object){
04 o = new obj.constructor();
05 }else{
06 o = new obj.constructor(obj.valueOf());
07 }
08 for(var key in obj){
09 if ( o[key] != obj[key] ){
10 if ( typeof(obj[key]) == object ){
11 o[key] = clone(obj[key]);
12 }else{
13 o[key] = obj[key];
14 }
15 }
16 }
17 o.toString = obj.toString;
18 o.valueOf = obj.valueOf;
19 return o;
20 }
方法3
view sourceprint?01 function clone(obj){
02 function Fn(){}
03 Fn.prototype = obj;
04 var o = new Fn();
05 for(var a in o){
06 if(typeof o[a] == "object") {
07 o[a] = clone(o[a]);
08 }
09 }
10 return o;
11 }