JSLint 的“语句位置中的意外表达式 'i'”是什么意思?
What does JSLint mean by 'Unexpected expression 'i' in statement position.'?
我在 JavaScript 中有一个 for 循环,我 运行 通过 JSLint 有几次。在过去我收到 the unexpected++ error
,我决定重构以使我的代码更具可读性。大约一个月后,JSLint 发布了更新,现在显示警告...
Unexpected expression 'i' in statement position.
for (i; i < scope.formData.tabs.length; i = i + 1) {
//See JSLint.com for why I pulled out i initialization and i = i+1 instead of i++
//and
var i = 0;
for (i; i < scope.formData.tabs.length; i += 1) {
scope.formData.tabs[i].show = false; // hide all the other tabs
if (scope.formData.tabs[i].title === title) {
scope.formData.tabs[i].show = true; // show the new tab
}
}
恢复到 var i = 0
和 i++
不会改善警告,JSLint 只是停止处理。
编辑: 看来我错了。有关更详细的解释,请参阅 ruffin 的回答。
似乎问题出在 jslint.com。请记住,它仍处于测试阶段。如果您使用旧版本 (old.jslint.com),问题似乎就消失了。
这是旧的输出。jslint.com:
这主要是关于未定义范围...等等
这是 jslint.com 的输出:
这是关于范围没有得到定义......等
Unexpected 'for'.
连同
Unexpected expression 'i' in statement position.
现在,我想你应该只使用旧版本 jslint.com。
看起来像 isn't [just?] due to JSLint being in beta. It's because Crockford no longer allows for
statements by default. Looks like I'm going to need to set aside a weekend to read the new instructions and source。伙计,Circle K 正在发生奇怪的事情。
The most important new feature of ES6 is proper tail calls. This has
no new syntax, so JSLint doesn't see it. But it makes recursion much more
attractive, which makes loops, particularly for loops, much less
attractive.
然后在 /*jslint */
指令部分的主要 table:
Description: Tolerate for
statement
Option: for
Meaning: true
if the for
statement should be allowed.
table下面有更多解释:
JSLint does not recommend use of the for
statement. Use array methods
like forEach
instead. The for
option will suppress some warnings. The
forms of for
that JSLint accepts are restricted, excluding the new ES6 forms.
因此,要在新的 JSLint 中创建此 lint,您至少需要 此代码(设置 for
指令):
/*jslint white:true, for:true */
/*global scope, title */
function test()
{
"use strict";
var i;
for (i=0; i < scope.formData.tabs.length; i = i + 1) {
scope.formData.tabs[i].show = false; // hide all the other tabs
if (scope.formData.tabs[i].title === title) {
scope.formData.tabs[i].show = true; // show the new tab
}
}
}
请注意,我 做了 仍然需要移动 i
的初始化,因此您可能仍然有一个值得 reporting. I'll also admit I'm with Stephen at the question you link 的问题;我不确定为什么 i+= 1
更好。但现在它看起来像是一个硬性要求。没有 plusplus
选项。
另请注意,如果代码未包装在函数中(我在上面的 test
中包装),您将得到 Unexpected 'for' at top level.
,这是一个新错误。
我在 JavaScript 中有一个 for 循环,我 运行 通过 JSLint 有几次。在过去我收到 the unexpected++ error
,我决定重构以使我的代码更具可读性。大约一个月后,JSLint 发布了更新,现在显示警告...
Unexpected expression 'i' in statement position. for (i; i < scope.formData.tabs.length; i = i + 1) {
//See JSLint.com for why I pulled out i initialization and i = i+1 instead of i++
//and
var i = 0;
for (i; i < scope.formData.tabs.length; i += 1) {
scope.formData.tabs[i].show = false; // hide all the other tabs
if (scope.formData.tabs[i].title === title) {
scope.formData.tabs[i].show = true; // show the new tab
}
}
恢复到 var i = 0
和 i++
不会改善警告,JSLint 只是停止处理。
编辑: 看来我错了。有关更详细的解释,请参阅 ruffin 的回答。
似乎问题出在 jslint.com。请记住,它仍处于测试阶段。如果您使用旧版本 (old.jslint.com),问题似乎就消失了。
这是旧的输出。jslint.com:
这主要是关于未定义范围...等等
这是 jslint.com 的输出:
Unexpected 'for'.
连同
Unexpected expression 'i' in statement position.
现在,我想你应该只使用旧版本 jslint.com。
看起来像 for
statements by default. Looks like I'm going to need to set aside a weekend to read the new instructions and source。伙计,Circle K 正在发生奇怪的事情。
The most important new feature of ES6 is proper tail calls. This has no new syntax, so JSLint doesn't see it. But it makes recursion much more attractive, which makes loops, particularly for loops, much less attractive.
然后在 /*jslint */
指令部分的主要 table:
Description: Tolerate
for
statement
Option:for
Meaning:true
if thefor
statement should be allowed.
table下面有更多解释:
JSLint does not recommend use of the
for
statement. Use array methods likeforEach
instead. Thefor
option will suppress some warnings. The forms offor
that JSLint accepts are restricted, excluding the new ES6 forms.
因此,要在新的 JSLint 中创建此 lint,您至少需要 此代码(设置 for
指令):
/*jslint white:true, for:true */
/*global scope, title */
function test()
{
"use strict";
var i;
for (i=0; i < scope.formData.tabs.length; i = i + 1) {
scope.formData.tabs[i].show = false; // hide all the other tabs
if (scope.formData.tabs[i].title === title) {
scope.formData.tabs[i].show = true; // show the new tab
}
}
}
请注意,我 做了 仍然需要移动 i
的初始化,因此您可能仍然有一个值得 reporting. I'll also admit I'm with Stephen at the question you link 的问题;我不确定为什么 i+= 1
更好。但现在它看起来像是一个硬性要求。没有 plusplus
选项。
另请注意,如果代码未包装在函数中(我在上面的 test
中包装),您将得到 Unexpected 'for' at top level.
,这是一个新错误。