var obj = {
id:1,
name:"fire",
use:{
age:18,
key:20,
},
color:["pink","red","blue"],
};
var o = {};
// //浅拷贝方法:只拷贝最外面一层,深层次只拷贝地址
// Object.assign(o,obj);
// console.log(o);
//深拷贝:拷贝多层,会全部开辟新空间,不会互相干扰
function DeepCopy(newobj,oldobj){
for(var k in oldobj)
{
var item = oldobj[k];
if(item instanceof Array)//必须先写数组,后写对象,因为数组也属于对象
{
newobj[k] = [];
DeepCopy(newobj[k],item);
}
else if(item instanceof Object)
{
newobj[k] = {};
DeepCopy(newobj[k],item);
}
else{
newobj[k] = item;
}
}
}
DeepCopy(o,obj);
o.use = {};
console.log(o);
console.log(obj);
相关文章
- 11-24使用Hutool来实现深拷贝
- 11-24【随记】实体对象深拷贝
- 11-24JS 深拷贝-策略模式实战
- 11-24ES6的解构赋值与深拷贝和浅拷贝
- 11-24远程登录和远程拷贝
- 11-24深拷贝(deep clone)与浅拷贝(shallow clone)
- 11-24浅谈.net平台下深拷贝和浅拷贝
- 11-24011、赋值=、浅拷贝copy、深拷贝deepcopy
- 11-24python深拷贝和浅拷贝
- 11-24JS深拷贝