对象中扩展运算符的作用

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 });

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);

对象中扩展运算符的作用_第1张图片

你可能感兴趣的:(JavaScript,前端,javascript,开发语言)