第一种方法
function getType( obj ){
return Object.prototype.toString.call( obj ).slice( 8, -1 )
}
function deepCopy( obj ){
let result,newObj = getType( obj )
if(newObj == "Object"){
result = {}
}else if(newObj == "Array"){
result = []
}else{
return obj
}
for(var i in obj){
let copyObj = obj[i]
if(getType( obj ) == "Object" || "Array"){
result[i] = deepCopy(obj[i])
}else{
result[i] = copyObj
}
}
return result
}
第二种方法
function deepCopy(obj) {
var newObj = null;
if (typeof (obj) == "object" && obj != null) {
newObj = obj instanceof Array && [] || {}
for (var i in obj) {
newObj[i] = deepCopy(obj[i])
}
} else {
newObj = obj
}
return newObj;
}