argumental reference inconsistency in javascript

匆匆过客 提交于 2019-12-02 09:35:47

Javascript always passes arguments by value, so this won't work:

 function foo(x) {
    x = 100;
 }

 y = 5
 foo(y)
 y == 100 // nope

However this does work:

 function foo(x) {
    x.bar = 100;
 }

 y = {}
 foo(y)
 y.bar == 100 // yes

In the second snippet x is still passed by value, but this very value is a reference (pointer) to an object. So it's possible in a function to dereference it and access what's "inside" the object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!