全局变量有什么不好? [英] What is so bad about global variables?

查看:117

如果采用良好的结构化方法,全局变量完全可以接受使用,并实际上节省了一些开销与所有传递/复制各种指针/变量。

对我来说,使用本地变量更多的是它的容易程度维护,调试和移植到其他系统的代码。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆