Javascript pointer/reference craziness. Can someone explain this?

前端 未结 5 1931
夕颜
夕颜 2020-11-30 18:14

Javascript passes objects by reference. This makes perfect sense. But once you start manipulating those objects, everything acts in a way that seem unintuitive. Let me offer

5条回答
  •  一整个雨季
    2020-11-30 18:58

    Objects in Javascript can exist by themselves without needing a name. For example:

    {}
    

    is a new instance of a dictionary object.

    a = {};
    

    creates a new dictionary object and makes a refer to it. Now

    b = a;
    

    makes b refer to the same underlying object. You can then make a point somewhere else:

    a = "hi";
    

    and b still points to the same dictionary object it did before. The behaviour of b is unrelated to how you change what a points to.

提交回复
热议问题