JSLint错误:将所有'var'声明移动到函数的顶部 [英] JSLint error: Move all 'var' declarations to the top of the function

查看:114
本文介绍了JSLint错误:将所有'var'声明移动到函数的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSLint站点已更新,我无法再检查JS脚本了。对我来说,这个警告并不重要,我不想通过数千行来解决这个问题,我想找到更多关键问题。

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?

Does anybody know how to turn off this error, or use legacy JSLint?

更新

示例:

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输出:

jslint.com output:

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).

问题:

将变量放在首位功能是新的要求。我无法使用JSLINT来测试代码,因为它会停止扫描此错误的脚本。

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.

我有很多代码,并且不希望将此警告威胁为严重错误。

I have a lot of code, and do not want to threat this warning as critical error.

更新2011年8月22日:找到 http://jshint.com ,它看起来比 http://jslint.com/

UPDATE 8/22/2011: found http://jshint.com, it looks much better than http://jslint.com/

推荐答案

2017年6月更新:受支持(例如,如果您未在Internet Explorer 10或更低版本中运行JavaScript),应该考虑使用 let 而不是 var

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 let instead of var.

例如: for(let i = 0; ...; i ++)

我无法从<$ c中输入 var i; $ c> for(var i = 0; ...; i ++)位于我的函数顶部。特别是当 JavaScript规范将其作为可接受的语法时 部分(12.6)。此外, Brendan Eich 在他的示例中使用了语法。

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 Specification has it as an acceptable syntax in the for section (12.6). Also, it's the syntax Brendan Eich uses in his examples.

将声明移到顶部的想法是,它应该更准确地反映在引擎盖下发生的事情,但这样做只会反映,而不会影响。

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.

对我来说,对于迭代来说,这是一个荒谬的期望。更重要的是因为JSLint在检测到它时会停止处理。

For me, this is a ridiculous expectation for for iterations. More so because JSLint stops processing when it detects it.

在函数顶部声明的变量是否更具可读性是值得商榷的。我个人更喜欢在使用它们时声明迭代器变量。我不在乎变量是否已经在内部创建,我在这里初始化它所以我很安全。

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.

现在,我正在使用 http://www.javascriptlint.com/online_lint.php 因为它似乎专注于重要的事情。

For now, I'm using http://www.javascriptlint.com/online_lint.php because it seems to focus on the important stuff.

这篇关于JSLint错误:将所有'var'声明移动到函数的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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