1. 当第一层的属性值不是基本数据类型时,可使用深拷贝
(1)JSON.parse(JSON.stringify(data))
let user = { name: '李四', skill: { css: 90 } };
let user1 = JSON.parse(JSON.stringify(user));
user1.name = "张三";
user1.skill.css = 60;
console.log(user, user1)
2. 当第一层的属性值是基本数据类型时,可使用浅拷贝
(1)Object.assign(target, ...sources) 第一个参数是拷贝的目标target,剩下的参数是拷贝的源对象sources(可以是多个)
let user = { name: '李四', skill: { css: 90 } };
let user1 = {};
Object.assign(user1, user);
user1.name = "张三";
user1.skill.css = 60;
console.log(user, user1);
(2)拓展运算符(...)
let user = { name: '李四', skill: { css: 90 } };
let user1 = { ...user };
user1.name = "张三";
user1.skill.css = 60;