Skip to content

Instantly share code, notes, and snippets.

@nowk
Last active August 29, 2015 14:00
Show Gist options
  • Save nowk/11387848 to your computer and use it in GitHub Desktop.
Save nowk/11387848 to your computer and use it in GitHub Desktop.
for loops and the crazy i
/* jshint node: true */
var i = 0;
var len = 5;
for(; i<len; i++) {
// a => 0, 1, 2, 3, 4
setTimeout(a.bind(null, i));
// b => 0, 1, 2, 3, 4
setTimeout(b(i));
// c => 5, 5, 5, 5, 5
setTimeout(function() {
c(i);
});
}
function a(i) {
console.log('a', i);
}
function b(i) {
return function() {
console.log('b', i);
};
}
function c(i) {
console.log('c', i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment