Skip to content

Instantly share code, notes, and snippets.

@Colorfulstan
Created September 6, 2015 19:15
Show Gist options
  • Save Colorfulstan/9226c44585091f3b6357 to your computer and use it in GitHub Desktop.
Save Colorfulstan/9226c44585091f3b6357 to your computer and use it in GitHub Desktop.
passing callbacks using a loop-iteration counter
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