Javascript - How to clone an object?

前端 未结 8 1915
悲&欢浪女
悲&欢浪女 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:00

    Your line myObjTwo = myObjOne does not clone myObjOne, it just creates a duplicate reference to the same object!

    The actual answer is to use a clone function, perhaps from a library such as underscore.js. But really, it looks like you have some reading and learning to do about the concept of objects and pointers in Javascript.

提交回复
热议问题