浅拷贝、深拷贝

//================================浅拷贝

//ES6属性的简洁表达式

var obj1 = { ...obj };

//ES6  Object.assign()

var obj2 = Object.assign({},obj);

//for in

for( let k in obj){
  obj3[k] = obj[k];
 }

//================================深拷贝

//JSON.parse(JSON.stringify())

var obj4 = JSON.parse(JSON.stringify(obj));

// 递归

function fun(obj){
var obj1 = {};
for(let k in obj){
if(typeof(obj[k]) === "object"){
obj1[k] = fun(obj[k]);
}else{
obj1[k] = obj[k];
}
}
return obj1;
}
var obj5 = fun(obj);
obj5.a = 123;
obj5.c.aa = 456;
console.log(obj5,obj);

posted @ 2019-12-11 20:25  casf  阅读(247)  评论(1编辑  收藏  举报