Skip to content

Instantly share code, notes, and snippets.

@lukewendling
Last active December 27, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukewendling/7324853 to your computer and use it in GitHub Desktop.
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
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”
@lukewendling
Copy link
Author

Object.keys is available in ES5 (works in Chrome / Node). Assuming this was the intent of the original 'f.length', which is otherwise undefined.

@lukewendling
Copy link
Author

perhaps the original 'f' object was supposed to be an array. rev 5 adds a closure to retain values in an async loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment