64

I can not get around JSHint's error message. Here is the loop I am using:

for (i = 0; i < Collection.length; i += 4) {
    data.push({
        items : Collection.slice(i, i + 4).map(function(item) {
            return {
                id: item[0],
                title: item[1],
            };
        })
    });
}
5
  • I am sorry, I have edited my code... i forgot to paste the loop section Commented Oct 26, 2012 at 7:07
  • Which kind of error are you getting? Commented Oct 26, 2012 at 7:07
  • Don't make functions within a loop thrown by JSHint Commented Oct 26, 2012 at 7:08
  • @OhadSchneider your link 404's Commented Jan 11, 2017 at 21:29
  • @Kev web.archive.org/web/20160414143531/http://jslinterrors.com/… Commented Jan 12, 2017 at 11:30

3 Answers 3

122

You can just move the function outside the loop and pass a reference to it to map:

function mapCallback(item) {
    return {
        id : item[0],
        title : item[1],
    };
}
for (i = 0; i < Collection.length; i += 4) {
    data.push({
        items: Collection.slice(i, i + 4).map(mapCallback)
    });
}

Alternatively, you can use a JSHint directive to ignore function expressions inside loops. Just put this at the top of the file in question:

/*jshint loopfunc: true */
Sign up to request clarification or add additional context in comments.

11 Comments

@Skyrim1 The reason for the warning is to avoid the bug frequently encountered if you were using i in an asynchronous callback (onclick or ajax). You're safe here, you can tell jshint to be quiet using James' suggestion
If anyone happens to be using Sublime Text with SublimeLinter, you can change this setting in Package Settings -> SublimeLinter -> User - Settings, with "jshint_options": { "loopfunc": true }
If you are disabling the warning using the /*jshint ... */ comment, it's safest to keep the comment inside that loop block instead of top of the file. This way you don't accidentally hide future issues in other parts of the file.
/*jshint loopfunc: true */ is not effective :/
@moala, I put /* jshint loopfunc: true */ as the first line inside my for loop, and the warning went away.
|
6

Declaring a function in a loop is messy, and potentially error prone. Instead, define the function once, and then enter the loop.

var objMaker = function(item) {
    return {
        id : item[0],
        title : item[1],
    };
};

for (i = 0; i < Collection.length; i += 4) {
    data.push({
                  items : Collection.slice(i, i + 4).map(objMaker)
             });
}

2 Comments

some commentary might help OP understand better
"messy, and potentially error prone" does not explain what might happen under which circumstances.
5

People say "Declaring a function in a loop is messy and potentially error-prone", but functions within loops is what directly instructed in, for example, Array.prototype.forEach method. Just because the word "function" should theoretically mean defining it anew in every forEach call it does not mean it is actually defined each time by the Javascript engine.

The same goes for outer loops since engines have "lazy" processing of instructions. They are not going to redefine the whole forEach/Map/etc construct-instruction anew if nothing really changed about it, they will just feed new arguments to it.

The times of ancient JS engines which were clueless about such simple things as well as of code context are long gone. And yet we are getting this ancient warning which was conceived when functions were not yet capable of being passed as arguments as in the cases of forEach or Map.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.