js对象或数组深复制
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>深拷贝</title> </head> <body> <script> var isArray = function (object) { return object && typeof object === 'object' && object.constructor === Array; } var deepCopy2=function (obj) { var result=obj.constructor===Object?{}:[]; for(key in obj){ if(!obj.hasOwnProperty(key))continue;//防止遍历原型上的属性 if( typeof obj[key]==='object'){ result[key]=deepCopy2(obj[key]); } else{ result[key]=obj[key]; } } return result; }; var x = { a: 1, b: ["a","b","c",{"key":"value"}] }; var arr=["a","bb","cc",["wo","shi","haoren"]]; var arr2=deepCopy2(arr); var arr3=deepCopy2(arr); arr2[0]="A"; </script> </body> </html>