javascript - setting global variables inside a function

后端 未结 3 979
悲&欢浪女
悲&欢浪女 2021-01-15 20:02

I am trying to create a function which will dynamically set the value of whatever global variable is passed as a parameter. It\'s not working, and I\'m trying to figure out

3条回答
  •  伪装坚强ぢ
    2021-01-15 20:58

    Javascript is pass-by-value. (Objects, arrays, and other non-primitives are passed by value-of-reference.) That means that the value of the variable (or reference) is passed to the function, but the function parameter does not become an alias for the actual argument. Thus, you cannot change a variable outside a function without referencing it (as you do in your last example).

    See this answer in another thread for more information.

提交回复
热议问题