let obj1 ={
age:10,
sex:"男",
car:["奔驰","宝马","奥迪","奥拓"],
dog:{
name:"大黄",
age:5,
color:"黑白色"
}
}
let obj2={}
function extend(a,b){
for(var key in a){
var item = a[key]
if(item instanceof Array){
b[key]=[]
extend(item,b[key])
}else if (item instanceof Object){
b[key]={}
extend(item,b[key])
}else{
b[key]=item
}
}
}
obj2.email="fy741@sina.com"
extend(obj1,obj2)
console.log(obj1)
console.log(obj2)
js学习笔记--深拷贝
最新推荐文章于 2024-10-08 16:34:35 发布