Skip to content

Instantly share code, notes, and snippets.

@Chris927
Created July 2, 2014 07:13
Show Gist options
  • Select an option

  • Save Chris927/64faacebb7a0cfcda56c to your computer and use it in GitHub Desktop.

Select an option

Save Chris927/64faacebb7a0cfcda56c to your computer and use it in GitHub Desktop.
Nice Little Javascript Scope Challenge
// credit: http://rzrsharp.net/2011/06/27/what-does-coffeescripts-do-do.html
var closures = [];
function createClosures() {
for (var i = 0; i < 5; i++) {
closures[i] = function() {
console.log("i=" + i);
}
}
}
function run() {
for (var i = 0; i < 5; i++) {
closures[i]();
}
}
createClosures();
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment