Problem at line 1 character 6: Move 'var' declarations to the top of the function.
I do prefer having all my vars at the top of the function, loop vars are an exception though. Problem at line 1 character 6: Move 'var' declarations to the top of the function.
I do prefer having all my vars at the top of the function, loop vars are an exception though.Declaration of variables in the loop could lead to this type of error:
http://cam.ly/blog/2011/01/javascript-interview-question/
(All of the 4 <li> will do alert('4') when clicked, when the programmer expects alert('1'), alert('2'), etc.)
If the variable had been declared at the top, that error probably would not happen.
addEventListener("stuff", function() {
return function(index) {
alert(index);
}
}(i);
is the fix, either way