Skip to content

Instantly share code, notes, and snippets.

@jedp
Created February 23, 2012 17:55
Show Gist options
  • Save jedp/1894033 to your computer and use it in GitHub Desktop.
Save jedp/1894033 to your computer and use it in GitHub Desktop.
javascript scoping example
var d1 = {};
var d2 = {};
for(var i=0; i<10; i++) {
d1[i] = function() { return i; };
}
for(var j=0; j<10; j++) {
(function() {
var k = j;
d2[j] = function () { return k; };
})();
}
console.log(d1[5]()); // -> 10
console.log(d2[5]()); // -> 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment