Skip to content

Instantly share code, notes, and snippets.

@gregmercer
Created July 24, 2012 21:51
Show Gist options
  • Save gregmercer/3172905 to your computer and use it in GitHub Desktop.
Save gregmercer/3172905 to your computer and use it in GitHub Desktop.
Javascript: closure example
function example() {
var o = {}, i = 0;
for (i = 0; i < 3; i++) {
o[i] = function() {
console.log(i);
};
}
o[0]();
o[1]();
o[2]();
}
example();
function example2() {
var o = {}, i = 0;
for (i = 0; i < 3; i++) {
o[i] = (function(value) {
return function() {
console.log(value);
}
})(i);
}
o[0]();
o[1]();
o[2]();
}
example2();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment