1.对象的引用赋值
// 1. 对象的引用赋值
const info = {name: "why",age: 18};
const obj = info;
info.name = "kobe";
console.log(obj.name); // kobe
2.对象的浅拷贝
// 2. 对象的浅拷贝
const info = {name: "why",age: 18,friend: {name: "kobe", age: 18}};
const obj = Object.assign({},info);
console.log(obj);
// { name: 'why', age: 18, friend: {… } }
info.name = "kobe";
console.log(obj.name); // why
info.friend.name = "james";
console.log(obj.friend.name); //james
// 引用lodash实现浅拷贝
const obj = _.clone(info);
3.对象的深拷贝
// 3. 对象的深拷贝
const info = {name: "why",age: 18,friend: {name: "kobe", age: 18}};
const obj = JSON.stringify(info);
console.log(obj);
// {"name":"why","age":18,"friend":{"name":"kobe","age":18}}
const obj = JSON.parse(JSON.stringify(info));
console.log(obj);
// {name: 'why', age: 18, friend: {…}}
info.friend.name = "james";
console.log(obj.friend.name); // kobe
// 引用lodash实现深拷贝
const obj = _.clonedeep(info);
|