Skip to content

Instantly share code, notes, and snippets.

@wangbinyq
Last active March 30, 2016 05:20
Show Gist options
  • Save wangbinyq/c3544b51b9bbf7f8d31e to your computer and use it in GitHub Desktop.
Save wangbinyq/c3544b51b9bbf7f8d31e to your computer and use it in GitHub Desktop.
var cbs = []
for(var i=0; i<10; i++) {
cbs.push(function(){console.log(i);});
}
for(var c=0; c<cbs.length; c++) { // index must not be *i*
cbs[c](); // output: 10 ... 10
}
//------
for(var i=0; i<10; i++) {
(function(x) {
cbs.push(function(){console.log(x);});
})(i);
}
for(var c=0; c<cbs.length; c++) {
cbs[c](); // output: 0 ... 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment