Why are global variables considered bad practice?

后端 未结 7 1618
野趣味
野趣味 2020-11-22 03:19

I keep seeing warnings not to use global variables in JavaScript, but it seems that the only reason people say that is because the clogs up the global namespace. I can imagi

7条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 03:51

    Global variables can significantly increase coupling, significantly reduces the scalability and testability of your code. Once you start using globals, you now have to know where and how the variable is modified (i.e. breaking encapsulation). Most of the literature and conventions out there will argue that performance is the least of your concerns when using globals.

    This is a fantastic article outlining why global variables cause headaches.

提交回复
热议问题