Javascript JSLint 错误:将所有“var”声明移至函数顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4646455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSLint error: Move all 'var' declarations to the top of the function
提问by Oleg Yaroshevych
JSLint site updated, and I cannot check JS scripts anymore. For me, this warning is not critical, and I don't want to go through thousands of lines to fix this, I want to find more critical problems.
JSLint 站点已更新,我无法再检查 JS 脚本了。对我来说,这个警告并不重要,我不想通过数千行来解决这个问题,我想找到更严重的问题。
Does anybody know how to turn off this error, or use legacy JSLint?
有人知道如何关闭此错误,或使用旧版 JSLint 吗?
UPDATE
更新
Example:
例子:
function doSomethingWithNodes(nodes){
this.doSomething();
for (var i = 0; i < nodes.length; ++i){
this.doSomethingElse(nodes[i]);
}
doSomething(); // want to find this problem
}
jslint.com output:
jslint.com 输出:
Error:
Problem at line 4 character 8: Move all 'var' declarations to the top of the function.
for (var i = 0; i < nodes.length; ++i){
Problem at line 4 character 8: Stopping, unable to continue. (44% scanned).
Problem:
问题:
Having variables on top of the functions is new requirement. I cannot use JSLINT to test code, because it stops scanning script on this error.
在函数之上有变量是新的要求。我不能使用 JSLINT 来测试代码,因为它停止扫描此错误的脚本。
I have a lot of code, and do not want to threat this warning as critical error.
我有很多代码,不想将此警告威胁为严重错误。
UPDATE 8/22/2011: found http://jshint.com, it looks much better than http://jslint.com/
2011 年 8 月 22日更新:找到了http://jshint.com,它看起来比http://jslint.com/好多了
回答by Lee Kowalkowski
Update June, 2017:Subject to support (e.g. if you're not running JavaScript in Internet Explorer 10 or below), you should look into using letinstead of var.
2017 年 6 月更新:受支持(例如,如果您不在 Internet Explorer 10 或更低版本中运行 JavaScript),您应该考虑使用let而不是var。
For example: for(let i=0; ...; i++)
例如: for(let i=0; ...; i++)
There's no way I'm going to put var i;
from a for(var i=0; ...; i++)
at the top of my functions. Especially when The JavaScript Specificationhas it as an acceptable syntax in the for
section (12.6). Also, it's the syntax Brendan Eichuses in his examples.
我不可能把var i;
afor(var i=0; ...; i++)
放在我的函数的顶部。特别是当JavaScript 规范在for
第 (12.6) 节中将其作为可接受的语法时。此外,这也是Brendan Eich在他的示例中使用的语法。
The idea of moving the declaration to the top is that it is supposed to more accurately reflect what happens under the hood, however, doing so will only reflect, not affect.
将声明移到顶部的想法是它应该更准确地反映幕后发生的事情,但是,这样做只会反映,而不是影响。
For me, this is a ridiculous expectation for for
iterations. More so because JSLint stops processing when it detects it.
对我来说,这是对for
迭代的荒谬期望。更是如此,因为 JSLint 在检测到它时会停止处理。
Whether having variables declared at the top of a function is more readable is debatable. I personally prefer iterator variables to be declared when they are used. I don't care if the variable is already created internally, I'm initialising it here so I am safe.
在函数顶部声明变量是否更具可读性是有争议的。我个人更喜欢在使用时声明迭代器变量。我不在乎变量是否已经在内部创建,我在这里初始化它所以我很安全。
I would argue that declaring an iterator variable where they are used ensures they are not accidentally made global (if you move the loop out into another function, the iterator variable moves with it). This is much more maintainable than having to maintain variable declarations at the top of functions.
我认为在使用它们的地方声明一个迭代器变量可以确保它们不会意外地成为全局变量(如果你将循环移到另一个函数中,迭代器变量也会随之移动)。这比必须在函数顶部维护变量声明更易于维护。
For now, I'm using http://www.javascriptlint.com/online_lint.phpbecause it seems to focus on the important stuff.
现在,我使用http://www.javascriptlint.com/online_lint.php因为它似乎专注于重要的东西。
回答by jjrv
Google Closure compiler will actually fail to correctly detect the type of the loop variable of a for...in loop unless it's declared like for (var i in ...) and no annotation seems to fix this, so the declaration cannot be moved to the top.
Google Closure 编译器实际上无法正确检测 for...in 循环的循环变量的类型,除非它声明为 for (var i in ...) 并且似乎没有注释可以解决此问题,因此无法移动声明到顶部。
回答by user123444555621
You can download legacy versionsanytime, or modify the latest version. It's not that hard, really (search for move_var
). Then run jslint locally, either using node, or using a browser with a simple HTML form - you may want to copy Crockford's original.
您可以随时下载旧版本,或修改最新版本。这并不难,真的(搜索move_var
)。然后在本地运行 jslint,要么使用 node,要么使用带有简单 HTML 表单的浏览器 - 您可能想要复制 Crockford 的原始版本。
Note that the warning was introduced as part of a major rewrite, and only occurs after for(
, so the message is a little misleading.
请注意,警告是作为主要重写的一部分引入的,并且只发生在 之后for(
,因此该消息有点误导。
回答by Paul Beusterien
Note that move all vars to the top is different from "allow one var statement per function". The requirement to move all variables to the top is new and doesn't seem to have a switch. More at http://groups.google.com/group/jsmentors/browse_thread/thread/5e90c25230f8e22/70e1a95a20fb829e
请注意,将所有变量移到顶部与“每个函数允许一个 var 语句”不同。将所有变量移到顶部的要求是新的,似乎没有开关。更多信息请访问 http://groups.google.com/group/jsmentors/browse_thread/thread/5e90c25230f8e22/70e1a95a20fb829e
回答by Simon Kenyon Shepard
I had this problem on my codebase, when we wanted to switch to the latest version of JSLINT. We had a lot of those and people were not happy about moving the declaration. We actually found the most elegant solution was to use underscore.js and instead of having the full verbose loop, to use the _.each() function, which removed the JSLint error and made our code more functional, cleaner, tighter and easier to read.
当我们想切换到最新版本的 JSLINT 时,我的代码库遇到了这个问题。我们有很多这样的人,人们对移动宣言并不满意。我们实际上发现最优雅的解决方案是使用 underscore.js 而不是使用完整的冗长循环,而是使用 _.each() 函数,它消除了 JSLint 错误并使我们的代码更实用、更清晰、更紧凑和更容易读。
回答by Peter
Even though the newbeta JSLintdoesn't document a comment directive for multiple var
tolerance within a function, it doesappear to support the directives from the original version.
尽管新的测试版JSLint没有记录var
函数内多重容差的注释指令,但它似乎确实支持原始版本的指令。
The original JSLintallowed you to do this:
原来的JSLint允许你这样做:
/*jslint vars: true */
In my experience this still works—I suppose for backwards compatibility. The time of this writing is June 2015.
根据我的经验,这仍然有效——我想是为了向后兼容。撰写本文的时间是 2015 年 6 月。
回答by Gio
I found that the following syntax will get the error removed:
我发现以下语法将删除错误:
function doSomethingWithNodes(nodes) {
this.doSomething();
var i; // HERE is where you move the 'var' to the top of the function
for (i = 0; i < nodes.length; ++i) {
this.doSomethingElse(nodes[i]);
}
doSomething(); // want to find this problem
}