Skip to content

Instantly share code, notes, and snippets.

@iam-peekay
Created April 27, 2016 23:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iam-peekay/330298dba53b5c6d60fe79f313b79e65 to your computer and use it in GitHub Desktop.
Save iam-peekay/330298dba53b5c6d60fe79f313b79e65 to your computer and use it in GitHub Desktop.
var result = [];
for (var i = 0; i < 5; i++) {
result[i] = (function inner(x) {
// additional enclosing context
return function() {
console.log(x);
}
})(i);
}
result[0](); // 0, expected 0
result[1](); // 1, expected 1
result[2](); // 2, expected 2
result[3](); // 3, expected 3
result[4](); // 4, expected 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment