When I assign a variable to another variable does it not link them together?

后端 未结 1 401
别那么骄傲
别那么骄傲 2021-01-28 04:26

I was trying to find out more about the lodash _.clone which I thought made a copy of the data in an object and created a different object. However when I was testing in the con

相关标签:
1条回答
  • 2021-01-28 05:27

    That's how variables work in JavaScript, and most languages. Assignment of b = a assigns the value of variable a to variable b. In languages where you're able to set a variable as a reference to another variable, there is usually a specific syntax for doing so; JavaScript does not have this feature.

    Note that this can appear confusing because, in the case of objects, the value being assigned from a to b is a reference to the object, but this still doesn't "link" the variables themselves, it just "points" them to the same object. Modifying either variable (via assignment) will not affect the other variable, but any changes the object through either variable will be mirrored by both variables because, again, they point to the same object.

    0 讨论(0)
提交回复
热议问题