I’m using the FB.Event.subscribe() observer model to find out when a user logs in. This method takes two arguments, a string containing the thing to watch, and callback function.
I’m following several events that handle the event the same way, so I’ve set up the callback function as a pre defined method and passed this to FB.Event.subscribe() like this:
Controller.prototype.go = function() {
FB.Event.subscribe('auth.login', this.fbHandleStatusChange);
FB.Event.subscribe('auth.logout', this.fbHandleStatusChange);
}
Controller.prototype.fbHandleStatusChange = function(response) {
// Doesn't work
this.otherFunction();
}
Controller.prototype.otherFunction = function() {
alert('hello');
}
Unfortunately this means that I loose access to ‘this’ within the scope of fbHandleStatusChange, obviously I don’t want to start coding references to concrete versions of Controller!
I’m guessing I’m passing the function incorrectly?
Thanks.
In JavaScript,
thisis defined entirely by how a function is called, not where it’s defined. This is different than some other languages. (JavaScript doesn’t have methods, it just has functions and some syntactic sugar that makes them look like methods sometimes.) So although you’re passing in your function correctly, Facebook doesn’t know about your object instance and can’t setthiscorrectly when calling your function.Check the
FB.Event.subscribedocs to see if it offers a way to say what “context” to use to call the event handler function. It may offer a way to do that. (This will usually be acontextorthisArgparameter.)If not, you can readily solve the problem with a closure:
That grabs a copy of
thisinto a variable calledself, which is used by thehandleChangefunction (which is a closure over the scope containing theselfvariable) to call your function with the correct context. More about closures here: Closures are not complicated More aboutthishere: You must rememberthisAlternately, though, are you really going to have multiple instances of
Controller? People coming to JavaScript from class-based languages tend to use constructor functions (a rough “class” analogue) unnecessarily. They’re the right choice if you need to have more than one instance of an object, but if you’re only ever going to have a singleControllerobject on the page, then using a constructor function and fiddling about withthisis overkill.If you don’t need multiple, independent
Controllerinstances, then:That creates a private scope via the outer anonymous function, and within that scope creates an instance (
inst) which we then return and refer to ascontrollerObject.controllerObjectin the above only has one property, the functiongo. All of our other functions are truly private. (I’ve also taken the liberty of ensuring that the functions have names, because that helps your tools help you.)Note that we don’t actually refer to
instanywhere in our function calls, because they’re all local to the closure scope. We can even have private data, by having othervars within the outer closure.