Created
February 23, 2012 17:55
-
-
Save jedp/1894033 to your computer and use it in GitHub Desktop.
javascript scoping example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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