mtfurlan mtfurlan - 11 months ago 48
Javascript Question

jslint warning "Don't make functions within a loop" only for functions referencing outside variables?

I have two anonymous functions created inside a loop, but

only gives a warning when the anon function references a variable not passed to it.

Why is this?

for(var i=0; i<5; ++i){
var age = 9001;
//This has a jshint warning, as it uses age
var cutoffs = [1800,12000].map(function(cutoff){
return cutoff < age;
//but this doesn't cause a warning, despite creating a function inside a loop
var cutoffs2 = [1800,12000].map(function(cutoff){
return cutoff < 42;

Answer Source

It is just a bad warning that should have been changed.

From the jshint issue I opened:

This warning was initially reported for all functions within a loop. It wasn't until gh-1887 (released in version 2.5.7) that the warning was limited to specific cases according to the bindings around which it closed. The text probably should have been updated at that time, as well.

It will be fixed by jshint pull request 3058, whenever that gets dealt with.