3 ms·
I think Crockford is right about that error. Declaration of variables in the loop could lead to this type of error: http://cam.ly/blog/2011/01/javascript-inte
by isak2 16y ago
I think Crockford is right about that error.
Declaration of variables in the loop could lead to this type of error:
http://cam.ly/blog/2011/01/javascript-interview-question/ 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.
- daleharvey 16y agoThat 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
- isak2 16y agoYeah. I meant that the mistake is less likely to have been made in the first place.