深拷贝
var obj ={
id:1,
name:'jack',
msg:{
age:18
},
color:['red','black']
}
var obj1={};
function fun(newobj,oldobj){
for(var key in oldobj){
//newobj[key]=oldobj[key];浅拷贝
//1.获取属性值
var item =oldobj[key];
//2.判断是否是数组
if(item instanceof Array){
newobj[key] = [];
fun(newobj[key],item) //item= color:['red','black']
}else if(item instanceof object){
//3.判断是否是数组
newobj[key] = {};
fun(newobj[key],item)
}else{
//4.基本数据类型
newobj[key]=item;
}
}
}
fun(obj1,obj)
obj1.id = 2;
obj1.msg.age = 20;
console.log(obj1,obj);