1.使用typeof区分基本数据类型和引用数据类型
2.使用Object.prototype.toString.call()来区分array和object
具体实现代码:
function deepclone(origin,target){ var obj_value = "[objcet objcet]"; toStr = Object.prototype.toString; for(var prop in origin){ if(origin.hasOwnProperty(prop)){ if(typeof origin[prop] =="object"){ if(toStr.call(origin[prop],null)==obj_value){ target[prop]={} }else{ target[prop]=[]; } deepclone(origin[prop],target[prop]); }else{ target[prop]=origin[prop]; } } } return target; } var obj ={ name:"chen", age:23, skill:["132",456,3543], habbits:{ name:"dsjak", age:"0" } } var obj1 = {}; deepclone(obj,obj1);