Skip to content

Instantly share code, notes, and snippets.

@rmcauley
Created May 19, 2016 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmcauley/9f19e22ca20ec10e03a76c8b2939c755 to your computer and use it in GitHub Desktop.
Save rmcauley/9f19e22ca20ec10e03a76c8b2939c755 to your computer and use it in GitHub Desktop.
// 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