Why are global variables considered bad practice?

2018-12-31 08:00发布

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 imagine this being easily fixed by putting all of the variables into one big object. Now the question is: are there any other reasons not to use global variables other than convenience sake? Are there any performance or compatibility issues involved with them?

7条回答
琉璃瓶的回忆
2楼-- · 2018-12-31 08:16

They clutter up the global namespace and are slower to look up than local variables.

First of all, having many global variables is always a bad thing because it's easy to forget you declared a variable somewhere and accidentally re-declare it somewhere else. If your first variable was local then you don't have a problem. If it was global, then it just got overwritten. This gets even worse when you get into implied globals (e.g. when you say someVar = someValue without declaring someVar with the var keyword).

Secondly, global variables take longer for Javascript to "find" than local variables. The difference in speed isn't huge, but it does exist.

For further reading and a more in-depth explanation of why globals are considered bad practice, you may want to check out this page.

查看更多
浮光初槿花落
3楼-- · 2018-12-31 08:17

Basically, because they can be accessed from any script on the page and because you can repeat it's name in the same scope. That's why a lot of Javascript engines use this code:

(function(){
    var foo = 'foo',//Local
    bar = 'bar';//Local
    window.globalVar = foo + bar;//Global
})();
alert(foo);//Error
alert(bar);//Error
alert(globalVar );//'foobar'
查看更多
牵手、夕阳
4楼-- · 2018-12-31 08:18

There shouldn't be any problem using global variables in your code as long as you are wrapping them inside a uniqe namespase/object (to avoid collision with scripts that are not yours)

There is one adventage of using global variable in javascript, and it derives from the fact that javascript is not a strong type language. there for, if you pass somes complex objects as arguments to a function, you will probebly lose all the intellisence for those objects (inside the function scope.) while using global objects insteads, will preserve that intellisence. and when you have intelisence, it actually can improve the debugging time (as opposed to what others said...)

I personally find that very usful and it certainly have place in my code.

(of course, one should alwayse make the right balance between locals and globals variables)

查看更多
残风、尘缘若梦
5楼-- · 2018-12-31 08:24

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.

查看更多
何处买醉
6楼-- · 2018-12-31 08:29

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.

查看更多
深知你不懂我心
7楼-- · 2018-12-31 08:36

The global variable which you have created might overwrite the existing window object properties. Because the global variables are accessing in global context i.e. window object.

查看更多
登录 后发表回答