jshint doesn't like the code below, and reports:
Functions declared within loops referencing an outer scoped variable may lead to confusing semantics. ($)
const suffix = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
for(let i=0; i<suffix.length; i++) {
(function(i) {
$('#foo'+i).click(function() {
$('#bar'+i).modal({show:true});
return false;
});
}(suffix[i]));
}
The code creates click handlers for fooA
through fooH
, and pops up modals barA
through barH
on the relevant click.
Granted, it is confusing, but is there a better way to do this, or should I just turn the warning off?