1 function clone(obj) { 2 var o; 3 if (typeof obj == "object") { 4 if (obj === null) { 5 o = null; 6 } else { 7 if (obj instanceof Array) { 8 o = []; 9 for (var i = 0, len = obj.length; i < len; i++) { 10 o.push(clone(obj[i])); 11 } 12 } else { 13 o = {}; 14 for (var j in obj) { 15 o[j] = clone(obj[j]); 16 } 17 } 18 } 19 } else { 20 o = obj; 21 } 22 return o; 23 }
用了自调用递归的方法,很6。