全局变量有什么不好? [英] What is so bad about global variables?
问题描述
在JavaScript圣经中,据说全局变量(不带var关键字的变量)是一件非常糟糕的事情。好吧,与强类型语言比如Java或C ++相比,JavaScript就像街头流氓俚语(was!)。
但是有没有任何性能问题?或者它导致浏览器崩溃?或者在使用全局变量时有一些明显的差异?
我学习了JavaScript,并遵循书籍和专家的指导原则,但是知道为什么这是一个非常重要的主题。
解决方案如果采用良好的结构化方法,全局变量完全可以接受使用,并实际上节省了一些开销与所有传递/复制各种指针/变量。
对我来说,使用本地变量更多的是它的容易程度维护,调试和移植到其他系统的代码。
Possible Duplicate:
Why is it bad to make elements global variables in Javascript?
In the JavaScript bible, it is said that global variables (variables declared without the "var" keyword) are a very bad thing. Ok, comparing to strongly typed languages like Java or C++ JavaScript is like a street gang slang (was!).
But are there any performance problems? Or is it causing the browser to crash? Or are there some actual noticeable difference when using globals?
I have learned JavaScript on my own and I follow the guidelines of the books and experts, but it will be nice to know why this is such an important subject.
If one takes a good structured approach, global variables are perfectly acceptable to use, and actually save a bit of overhead with all the passing around / copying of various pointers / variables.
For me, using local vars is more a matter of how easy it makes the code to maintain, debug, and port to other systems.
这篇关于全局变量有什么不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!