5 ms·
Is there any reference for the reasoning behind error-ing on the standard for loop?
by jacobbijani 16y ago
Is there any reference for the reasoning behind error-ing on the standard for loop?
- daleharvey 16y agoProblem 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.
- isak2 16y agoI 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.
- callahad 16y agoIt's concerned about the declaration of i within the loop's first term. Since JavaScript only has function-level scope, that variable leaks into the broader context, which can lead to unexpected behavior.
- marijn 16y agoNot if you actually, you know, know what you're doing. This dumbing down of programming does not work--if you have clueless people working on code, it'll end up broken, no matter how many tool-enforced 'best practices' they follow.
- Skalman 16y agoI can't see why people not liking the defaults (which are great for beginners) can't change their settings.
- evgen 16y agoWhy would the zen master of JavaScript of which you speak have any need for JSLint in the first place? Us mere mortals are sometimes forgetful and need reminders...
- deleted 16y ago[deleted]