Created
October 2, 2011 21:36
-
-
Save mattpat/1257994 to your computer and use it in GitHub Desktop.
How to avoid accidental closures in loops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeAlerter(x){ | |
return function(e){ | |
alert(x); | |
}; | |
} | |
for (var i = 0; i < 10; i++) | |
elements[i].addEventListener('click', makeAlerter(i)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment