I'm using a jQuery .each()
call, which uses an anonymous function, from inside a for
loop. JSLint comes up with the warning "Don't make functions within a loop".
Here's a code snippet from a larger function - in essence it is doing a check of whether each player of the game is still "alive" (has at least one piece on the board).
for( i=0 ; i<PLAYERLIMIT ; ++i ) {
if( player[i].status !== 0 ) { //skip already dead players
var stillAlive = false;
$board.find("td").each( function() { //this func causes JSLint warning
if( $(this).data("owner") === player[i].number ) {
stillAlive = true;
return false;
}
});
if( !stillAlive ) {
//... action to take for dead players
}
}
}
I can see how to remove this warning - just declare the function seperately and call it. But this is a very small one-shot function and essentially I consider this the body of a nested for
loop, (I'm essentially reading the .each()
call as something like for $("td") in $board {}
)
Is this JSLint providing one of it's style warning, or is this more serious?
Basically, is it best for me to fix this?
I'd like to understand the reason for the warning, so any comments RE the why the warning exists would be useful (again I want to know if it's practical or style).