Skip to content

Instantly share code, notes, and snippets.

@keriati
Forked from w0rm/callbackhell.js
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keriati/9618539 to your computer and use it in GitHub Desktop.
Save keriati/9618539 to your computer and use it in GitHub Desktop.
var AwesomeObject = function() {
this.init()
};
AwesomeObject.prototype = {
init: function() {
_.bindAll(this, 'onSuccess', 'onError', 'onComplete')
// or
this.onSuccess = $.proxy(this.onSuccess, this)
this.onError = $.proxy(this.onError, this)
this.onComplete = $.proxy(this.onComplete, this)
// or
this.onSuccess = this.onSuccess.bind(this)
this.onError = this.onError.bind(this)
this.onComplete = this.onComplete.bind(this)
// What do we do here?
this.funky = new FunkyObject();
},
/**
* 1000 lines of code
*
*
*
*/
doSomeThingAsync: function() {
// other developer can't see anymore the bind, maybe won't expect it? :(
return asyncThing({
success: this.onSuccess,
error: this.onError,
complete: this.funky.doThings
});
},
onSuccess: function() {
this.happy();
},
onError: function() {
this.sad();
},
onComplete: function() {
this.whatever();
},
happy: function() { console.log('happy'); },
sad: function() { console.log('sad'); },
whatever: function() { console.log('whatever'); }
}
@keriati
Copy link
Author

keriati commented Mar 18, 2014

From Andrey Kuzmin:

Doesn't seem ambiguous. If 'this' is used to call other object's methods, then its clear to assume that they are bound to this object.
It is clear to me that "this" in every function inside AwesomeObject.prototype namespace should refer to object instance.

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