JavaScript 中每个函数一个 var?
我一直在使用 JSLint 让我对我的 JavaScript 感觉很糟糕。 顺便说一句,这很棒。 有一项检查我不太明白,请您发表意见。
来自 jslint.com:
在具有块作用域的语言中,通常建议在首次使用的位置声明变量。 但由于 JavaScript 没有块作用域,因此更明智的做法是在函数顶部声明函数的所有变量。 建议每个函数使用单个 var 语句。
最后一句粗体的真正含义是什么? 我想我应该像这样声明多个变量?
var foo = 1, bar = 2;
而且,“明智”的部分只是一种编程风格,以防止出现错误,还是还有更多的内容?
感谢您的帮助。
I've been using JSLint to make me feel bad about my JavaScript. It is great, by the way. There is one check that I don't quite understand and I'd like your views, please.
From jslint.com:
In languages with block scope, it is usually recommended that variables be declared at the site of first use. But because JavaScript does not have block scope, it is wiser to declare all of a function's variables at the top of the function. It is recommended that a single var statement be used per function.
What is the last sentance in bold really saying? I think I should be declaring multiple variables like this?
var foo = 1, bar = 2;
And, is the "wise" part just a programming style to discourage errors down the line or is there more to it than that?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是,无论您是否意识到,JavaScript 都会无形中将所有 var 声明移动到函数作用域的顶部。
因此,如果您有这样的函数,则
警报窗口将包含未定义的内容。 因为在内部,已经改成了这样:
这叫“吊装”。
crockford 如此强烈提倡将 var 声明放在顶部的原因是,它使代码明显地匹配它将要执行的操作,而不是允许发生看不见的和意外的行为。
The problem is that, whether you realise it or not, javascript invisibly moves all the var declarations to the top of the function scope.
so if you have a function like this
the alert window will contain undefined. because internally, it's been changed into this:
this is called "hoisting".
The reason crockford so strongly advocates var declarations go at the top, is that it makes the code visibly match what it's going to do, instead of allowing invisible and unexpected behavior to occur.
基本上在 JavaScript 块 (
{ ... }
) 中不会引入新的作用域,只有函数作用域,因此不会在任何其他语句上创建作用域。在函数中任何位置引入的变量在函数中的任何位置都可见。
例如:
在具有块作用域的语言中,建议在第一次使用时声明变量,但由于 JavaScript 没有块作用域,因此最好在函数的顶部声明函数的所有变量。
查看这篇文章。
Basically in JavaScript blocks (
{ ... }
) do not introduce a new scope, there is only function-scope, so no scope is created on any other statement.A variable introduced anywhere in a function is visible everywhere in the function.
For example:
In languages with block scope, it recommended to declare the variables at the point of first use, but since JavaScript does not have block scope, it is better to declare all of a function's variables at the top of the function.
Check this article.
缺乏块作用域解释了下面的代码:
在大多数 C 风格(如语法)语言中,
var a = 2
定义只会为if 的范围定义“a”
块。 在函数顶部使用单个 var 语句有助于避免 Javascript 的这种怪癖,这种怪癖并不总是像上面那样明显,并且对于 C/C#/Java 程序员来说是意想不到的。The lack of block scope explains the code below:
In most C-style (as in syntax) languages, the
var a = 2
definition would define 'a' only for the scope of theif
block. Using a single var statement at the top of the function helps to avoid this quirk of Javascript, which is not always as obvious as the above, and would be unexpected to C/C#/Java programmers.是的,这意味着您在函数的开头声明所有变量。 无论您想在一行还是多行中执行此操作,都是一个选择问题。
原因在你提到的段落中有解释。 Javascript 变量只有函数级别的作用域。 如果您在 if/while/for 块内声明相同的变量,它将被新值覆盖,因为该块不带有新的作用域。 这与Java等语言不同。 为了避免这种意外,请在函数开头声明要在函数中使用的所有变量,这样就不会意外“重新声明”任何东西。
Yes, it means that you declare all variables at the beginning of the function. Whether you want to do it in one line or multiple lines is a matter of choice.
The reason is explained in the paragraph you mentioned. Javascript variables only have function level scope. If you declare the same variable inside an if/while/for block, it will be overwritten by the new value since the block doesn't carry a new scope. This is different from languages such as Java. To avoid such surprises, declare all the variables you are going to use in the function at the beginning of function so that you don't accidentally 'redeclare' andything.