let obj1=obj;和let obj1={...obj}的区别

    let obj={
        name:"吴小明",
        age:24,
        love:{
            pingpang:true
        }
    }
    let obj1=obj;
    obj.name="孙艺珍";
    obj.love.pingpang=false;
    console.log(obj)
    console.log(obj1)

输出结果为:

{name: "孙艺珍", age: 24,love: {pingpang: false}}
aa.html:20 {name: "孙艺珍", age: 24,love: {pingpang: false}}

 

    let obj={
        name:"吴小明",
        age:24,
        love:{
            pingpang:true
        }
    }
    let obj1={...obj};
    obj.name="孙艺珍";
    obj.love.pingpang=false;
    console.log(obj)
    console.log(obj1)

输出结果为:

{name: "孙艺珍", age: 24,love: {pingpang: false}}
aa.html:20 {name: "吴小明", age: 24,love: {pingpang: false}}

 

所以,let obj1={...obj};的意思是如果obj只有一层数据结构,这样复制的对象没有引用关系。

posted @ 2020-02-28 22:43  吴小明-  阅读(2501)  评论(0编辑  收藏  举报