1.对象的合并
let o1 = {
name: "张三",
age: 18,
brother: {
name: "李四",
age: 19,
},
};
//属性不重复
let o2 = {
hobby: "打篮球",
};
console.log({ ...o1, ...o2 });
//属性重复,后面对象的属性会覆盖前面的属性
let o3 = {
name: "王五",
hobby: "打篮球",
};
console.log({ ...o1, ...o3 });
console.log({ ...o3, ...o1 });
![](https://i-blog.csdnimg.cn/blog_migrate/637a44665b8178b80c6b5cdeb40844e7.png)
2.对象的拷贝
let o1 = {
name: "张三",
age: 18,
brother: {
name: "李四",
age: 19,
},
};
//浅拷贝,两个是不同的对象
let o4 = { ...o1 };
console.log(o4 == o1);
o4.age = 20;
o4.brother.age = 21;
console.log(o4);
console.log(o1);
//赋值是引用,指向的是同一个对象
let o5 = o1;
console.log(o5 == o1);
![](https://i-blog.csdnimg.cn/blog_migrate/822fae79b9398bff77b16c644340a524.png)