Created
May 19, 2016 07:35
-
-
Save rmcauley/9f19e22ca20ec10e03a76c8b2939c755 to your computer and use it in GitHub Desktop.
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
// ye olde ie8-compatible way | |
function setupTheThing(thing, i) { | |
thing.addEventListener("click", function() { console.log(i); }); | |
} | |
var i, thing; | |
for (i = 0; i < 5; i++) { | |
thing = document.getElementById("thing" + i); | |
setupTheThing(thing, i); | |
} | |
// yer shiny new JS2015 way | |
for (i = 0; i < 5; i++) { | |
let ii = i; // "let" is a 'private' variable | |
let thing = document.getElementById("thing" + ii); | |
thing.addEventListener("click", function() { console.log(ii); }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment