-
-
Save keriati/9618539 to your computer and use it in GitHub Desktop.
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
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'); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.