javascript,为什么不删除 var 关键字?
几乎所有的 javascript 书籍都这么说
当你 声明变量,因为没有 var,变量将被声明为 全局变量。
那么,为什么不删除 var 关键字,将默认声明设置为本地范围呢? 就像Python一样,如果你想使用全局变量,你可以这样写:
global foo;
我们几乎一直使用局部变量,不是吗? 有充分的理由吗? 感谢您的任何帮助。
编辑: 感谢您的所有帮助,我认为一定有充分的理由表明使用 var 更好,所以我并没有试图改变语言的样子。
almost all javascript books said that
always use var keyword when you
declare variables, because without
var, the variable will be declared as
global variable.
then, why not remove var keyword, make default declaration as local scope?
like Python, if you want to use global variables, you write:
global foo;
we use local variables almost all the time, don't we?
is there a good reason?
Thanks for any help.
edit:
Thanks for all your help, I thought there must be a good reason shows that using var is better, so I was not attempting to change the language what it was like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一点是没有简单的方法可以从 JavaScript 中删除某些内容。每个功能(甚至黑客)都已在数千个网站上使用,删除功能将破坏这些网站。 JavaScript只能被扩展。或者应该创建新的 JS,这将与以前的版本不兼容。
Another point is there is no easy way to remove something from JavaScript. Every feature (even hacks) is already used on thousands of sites and removing features will break those sites. JavaScript can only be extended. Or new JS should be created which will be incompatible with previous versions.
如果选择是“最窄范围”或“全局范围”,那么这是不可能的。
If the choice was "Narrowest scope" or "Global scope" then this wouldn't be possible.
这就是该语言的设计方式。如果变量是自动分配的,那么它将在全局范围内分配。
如果你仔细想想,这很有意义。变量应该分配在什么范围内?编译器无法通过显式声明来了解程序员的目标
JS 的设计方式是好是坏。我相信允许变量自动声明是一个错误,但考虑到它存在于 JS 中,让它们成为全局变量是有意义的,因为 JS 没有块级作用域。
This is just the way the language was designed. If a variable is auto-allocated it is allocated in the global scope.
It makes a lot of sense if you think about it. What scope should a variable be allocated in? The compiler has no way of knowing the programmers goal with an explicit declaration
For better or worse JS was designed the way it was. I believe allowing variables to auto-declare was a mistake but given that it exists in JS it makes sense to have them be global since JS does not have block level scope.
在函数内使用 var 关键字在局部范围内声明变量,从而防止覆盖任何全局变量。想法是安全起见,并使用 var。如果您知道自己在做什么(100% 确定您不会覆盖任何全局变量),请随意丢弃 var。
using var keyword inside a function declares the variable in local scope, hence preventing overwriting of any global variable. Idea is to play safe, and use var. If you know what you are doing (100% sure that you will not overwrite any global variable) feel free to discard var.
好的,我会再次尝试解释它是如何工作的。有一个 ECMAScript 的 Global 对象,它是其他一切的“根”。在浏览器中,
window
对象实现了Global
。所以:女士结论:JavaScript 是一种独特的语言,具有自己的特定特征,因此不要将其他语言知识应用于 JS(这会让 Douglas Crockford 成为一只悲伤的熊猫:)
OK, i'll try to explain how it works again. There is a ECMAScript's
Global
object, which is "root" of everything else. In browserswindow
object implementsGlobal
. So:Mrs Conclusion: JavaScript is distinct language with own specific traits, so do not apply other language knowledge to JS (it makes Douglas Crockford a sad panda :)