Last active
December 27, 2015 12:19
-
-
Save lukewendling/7324853 to your computer and use it in GitHub Desktop.
let's assume the original f object was supposed to be an array
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
function makeFun() { | |
var f = []; | |
// closure to freeze vars in async loop | |
var sum = function (n, m) { | |
return function () { | |
console.log("sum=" + (n + m)); | |
} | |
}; | |
for( var i=0 ; i<3 ; i++ ) { | |
f[i] = sum(i, f.length); | |
} | |
return f; | |
} | |
makeFun()[0]() | |
makeFun()[1]() | |
makeFun()[2]() | |
// Expected Results: | |
// makeFun()[0]() should alert “sum=0” | |
// makeFun()[1]() should alert “sum=2” | |
// makeFun()[2]() should alert “sum=4” |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
perhaps the original 'f' object was supposed to be an array. rev 5 adds a closure to retain values in an async loop.