That error would have happened despite if the var was declared at the top or in the loop
addEventListener("stuff", function() {
return function(index) {
alert(index);
}
}(i);
is the fix, either way