JSLint由' i'表示什么?在声明位置.&#39 ;? [英] What does JSLint mean by 'Unexpected expression 'i' in statement position.'?

查看:81
本文介绍了JSLint由' i'表示什么?在声明位置.&#39 ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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"在语句位置.对于(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.

推荐答案

类似于

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 语句
选项:用于
含义:如果应该允许 for 语句,则为 true .

表格下方有更多解释:

JSLint不建议使用 for 语句.使用数组方法就像 forEach 一样. for 选项将禁止显示某些警告.这JSLint接受的 for 形式受限制,但不包括新的ES6形式.

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中制作此皮棉,您至少需要 该代码(设置了 for 指令):

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 
        }
    }
}

请注意,我 did 仍然必须移动 i 的初始化,因此您可能仍然有一个值得我与史蒂芬(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由&amp;#39; i&amp;#39;表示什么?在声明位置.&amp;#39 ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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