Why are global variables considered bad practice?

后端 未结 7 1441
野趣味
野趣味 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

    If your script is very long and you use these variables from lots of functions it will increase your debugging time since the value of the global variable could have been changed from anywhere so if you are tracking where this changed to a non-expected value you'll have to check them all.

    This scenario is even more painful if different programmers modify this variable from other scripts included in your page.

提交回复
热议问题