Javascript - How to clone an object?

前端 未结 8 1925
悲&欢浪女
悲&欢浪女 2021-02-18 22:28

I am confused. I create a copy from myObjOne, than i delete an entry from myObjOne and JS delete the entry in my copy(myObjTwo) too? But w

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-18 23:12

    You can use Object.assign() but be aware of browser support.

    More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign.

    Example:

    myObjTwo = Object.assign({}, myObjOne);
    

提交回复
热议问题