手动实现深拷贝
function deepCopy(oldObj,newObj){
var newObj=newObj||{};
for(var key in oldObj){
debugger
if(typeof oldObj[key]==='object'){
if(oldObj[key].constructor === Array){
//这是数组
newObj[key]=[];
}else{
//这是对象
newObj[key]={};
}
deepCopy(oldObj[key],newObj[key]);
}else{
newObj[key]=oldObj[key];
}
}
return newObj;
}
let obj = {name:'乔巴',type:{name:'驯鹿'},age:'18'}
let deepObje = deepCopy(obj,{})
网友评论