Created
September 19, 2013 16:38
-
-
Save christophercliff/6626134 to your computer and use it in GitHub Desktop.
Function.prototype.bind vs fn(callback, ctx)
This file contains hidden or 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
this.myMethod(callback.bind(this)) | |
// vs. | |
this.myMethod(callback, this) | |
// In the former, context is in the hands of the user. In the latter, the context is the responsibility of the owner of `myMethod`. Every method in that API that accepts a callback now requires a bunch of boilerplate, e.g.: | |
myMethod: function (callback, ctx) { | |
ctx = (ctx || null) | |
// do stuff... | |
return callback.apply(ctx, args) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment