Javascript pointer/reference craziness. Can someone explain this?

前端 未结 5 1936
夕颜
夕颜 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 19:10

    Think of the anonymous object as itself having a name:

    a = {}; // The variable "a" now points to (holds) an anonymous object.
    b = a; // "b" points to the same anonymous object held by "a".
    a = 123; // "a" now holds some other value.
    b; // "b" still holds the anonymous object.
    

    The key is to remember that variables hold references to objects, not references to other variables. And the same object may be referred to by any number of variables.

提交回复
热议问题