Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

javascript - What does JSLint mean by 'Unexpected expression 'i' in statement position.'?

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

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

Reverting to var i = 0 and i++ does not get improve the warnings, JSLint just stops processing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

Then this in the /*jslint */ directive section's main table:

Description: Tolerate for statement
Option: for
Meaning: true if the for statement should be allowed.

There's a little more explanation below the 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.

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...