JSLint在语句位置的'意外表达'是什么意思。'? [英] What does JSLint mean by 'Unexpected expression 'i' in statement position.'?
问题描述
我在JavaScript中有一个for循环,我已经通过JSLint运行了几次。在过去我收到意外的++错误
,我决定重构以使我的代码更具可读性。大约一个月后,JSLint发布了更新,现在显示警告...
I have a for loop in JavaScript that I have run through JSLint a few times. In the past I received the unexpected++ error
, I decided to refactor to make my code more readable. A month or so later JSLint came out with an update and is now showing the warning...
语句位置的意外表达式'i' 。
for(i; i< scope.formData.tabs.length; i = i + 1){
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 http://stackoverflow.com/questions/3000276/the-unexpected-error-in-jslint
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只是停止处理。
Reverting to var i = 0
and i++
does not get improve the warnings, JSLint just stops processing.
推荐答案
看起来像后续问题不是[只是?],因为JSLint处于测试阶段。 这是因为默认情况下Crockford不再允许用于
语句。看起来我需要预留一个周末来阅读新说明和来源。在圈子K,男人正在发生奇怪的事情。
Looks like the follow-up problem 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. Strange things are afoot at the Circle K, man.
ES6最重要的新功能是正确的尾部调用。这有
没有新语法,所以JSLint没有看到它。但它使得递归更有吸引力,这使得循环,尤其是循环,更有吸引力的
。
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 * /
指令部分的主表中:
Then this in the /*jslint */
directive section's main table:
描述:容纳
语句
选项:for
含义:true
如果应该允许声明。
Description: Tolerate
for
statement
Option:for
Meaning:true
if thefor
statement should be allowed.
表格下面还有一些解释:
There's a little more explanation below the table:
JSLint不建议使用
表示
语句。使用数组方法
,例如forEach
。 选项的将禁止显示某些警告。 JSLint接受的
的
受限制,不包括新的ES6表格。
形式为
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,你需要至少这个代码(用于
指令集):
So to make this lint in the new JSLint, you need at least this code (with the for
directive set):
/*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
的初始化,因此您可能仍然遇到一个值报告。我也会承认我在Stephen的链接问题上与Stephen合作;我不确定为什么 i + = 1
更好。但现在它看起来像一个艰难的要求。否 plusplus
选项。
Note that I did still have to move i
's initialization, so you might still have an issue worth reporting. I'll also admit I'm with Stephen at the question you link; I'm not sure why i+= 1
is better. But now it looks like a hard requirement. No plusplus
option.
另请注意,如果代码未包含在函数中(我包含在 test
,上面),你会得到顶级意外'for'。
,这是一个新错误。
Notice also that if the code isn't wrapped in a function (I wrapped in test
, above), you'll get Unexpected 'for' at top level.
, which is a new error.
这篇关于JSLint在语句位置的'意外表达'是什么意思。'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!