Created
September 6, 2015 19:15
-
-
Save Colorfulstan/9226c44585091f3b6357 to your computer and use it in GitHub Desktop.
passing callbacks using a loop-iteration counter
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 create_callback(i){ | |
return function callback(callbackParams) { | |
// do something with the "right" i | |
} | |
} | |
// create closures with the temporary i and store them for later | |
var funcs = []; | |
for (var i = 0; i < 5; i++) { | |
funcs[i] = create_callback(i); | |
} | |
// give the stored closures with encapsulated i as callbacks | |
for (var j = 0; j < 5; j++) { | |
giveMeACallback( funcs[j] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment