Skip to content

Instantly share code, notes, and snippets.

@mainiak
Last active December 11, 2015 11:48
Show Gist options
  • Save mainiak/4596393 to your computer and use it in GitHub Desktop.
Save mainiak/4596393 to your computer and use it in GitHub Desktop.
var max = 10,
x = {vs:') versus '};
for (var i=0; i<max; i++) {
console.log('i: ', i);
setTimeout((function(number) { console.log('i(' + i + this.vs + number); }).bind(x), (i * 500), i);
}
@synaptiko
Copy link

What about this?

var max = 10;
var x = {
  tpl: 'x.i:',
  fnFactory: function(i) {
    var me = this;
    return function() {
      console.log(me.tpl, i);
    }
  }
};

for (var i=0; i<max; i++) {
  console.log('i:', i);
  setTimeout(x.fnFactory(i), (i * 500));
}

Or this in coffeescript?

max = 10
x = {
  tpl: 'x.i:'
  fnFactory: (i) ->
    me = @
    -> console.log(me.tpl, i);
}

for i in [0...max]
  console.log 'i:', i
  setTimeout x.fnFactory(i), i * 500

Is it more readable/understandable? :-)

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