js对象的深度克隆
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>js对象的深度克隆</title> 7 </head> 8 9 <body> 10 <script type="text/javascript"> 11 var cloneObj = function(obj) { 12 var str, newobj = obj.constructor === Array ? [] : {}; 13 if (typeof obj !== 'object') { 14 return; 15 } else if (window.JSON) { 16 str = JSON.stringify(obj), //系列化对象 17 newobj = JSON.parse(str); //还原 18 } else { 19 for (var i in obj) { 20 newobj[i] = typeof obj[i] === 'object' ? 21 cloneObj(obj[i]) : obj[i]; 22 } 23 } 24 return newobj; 25 }; 26 </script> 27 </body> 28 29 </html>