一、序列化转成json字符串深拷贝
let source = {"id":1};
// 序列化转成json字符串
let jsonStr = JSON.stringify(source)
// 转为对象
let target = JSON.parse(jsonStr);
二、Object.assign()深拷贝
var obj = {
name: 'zs',
age: 18
}
var obj_2 = Object.assign({}, obj);
obj.age = 20;
console.log(obj);
console.log(obj_2);
三、循环递归深拷贝
var obj = {
name: 'zs',
age: 18,
sex: '男'
}
var obj_3 = {};
for ( key in obj) {
obj_3[key] = obj[key];
}
obj.age = 20;
console.log(obj);
console.log(obj_3);
四、扩展运算符
var obj_4 = {...obj};
delete obj.age;
console.log(obj);
console.log(obj_4);