I've Heard Global Variables Are Bad, What Alternative Solution Should I Use?

后端 未结 9 672
暖寄归人
暖寄归人 2020-11-22 03:26

I\'ve read all over the place that global variables are bad and alternatives should be used. In Javascript specifically, what solution should I choose.

I\'m thinking

9条回答
  •  遥遥无期
    2020-11-22 03:38

    The issue with your solution is that it just makes you code harder to understand while still keeping all the downsides of global variables. The page you linked to covers the problems. The only problem your proposed solution really solves is namespace pollution but at the cost of not being able to see what global variables are declared as easily as the declaration is a function call).

    The solution is to write code without global variables. If a function needs a value pass it as a argument.

提交回复
热议问题