深拷贝-浅拷贝-引用赋值的关系

  <script>
    const info ={
      name:"hdc",
      age:21,
      friends:{
        name:"kobe"
      }
    }
    //1.引用赋值
    const obj1 = info
    // 2.操作二:浅拷贝
    const obj2 = {...info}
    //obj2.name = "james"
    //obj2.friend.name = "james"
    // console.log(info.friends.name)
    const obj3 = Object.assign({},info)
    //obj3.name = "curry"
    obj3.friends.name = "curry"
    console.log(info.friends.name)
    // 3,操作三:深拷贝
    //1.JSON方法实现深拷贝
    const obj4 = JSON.parse(JSON.stringify(info))
    //特殊的值是无能为力的
    //2.自己编写深拷贝函数(第三方库)
  </script>
posted @ 2024-10-23 12:09  韩德才  阅读(12)  评论(0)    收藏  举报