Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:55:33+00:00 2026-05-22T00:55:33+00:00

I’m using the FB.Event.subscribe() observer model to find out when a user logs in.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:55:34+00:00Added an answer on May 22, 2026 at 12:55 am

    In JavaScript, this is 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 set this correctly when calling your function.

    Check the FB.Event.subscribe docs 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 a context or thisArg parameter.)

    If not, you can readily solve the problem with a closure:

    Controller.prototype.go = function() {
        var self = this;
    
        FB.Event.subscribe('auth.login', handleChange);
        FB.Event.subscribe('auth.logout', handleChange);
        function handleChange() {
            return self.fbHandleStatusChange();
        }
    }
    

    That grabs a copy of this into a variable called self, which is used by the handleChange function (which is a closure over the scope containing the self variable) to call your function with the correct context. More about closures here: Closures are not complicated More about this here: You must remember this

    Alternately, 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 single Controller object on the page, then using a constructor function and fiddling about with this is overkill.

    If you don’t need multiple, independent Controller instances, then:

    var controllerObject = (function() {
        var inst = {};
    
        inst.go = go; // Make `go` a publicly-accessible function of the object
        function go() {
            FB.Event.subscribe('auth.login', fbHandleStatusChange);
            FB.Event.subscribe('auth.logout', fbHandleStatusChange);
        }
    
        // This is private to us, so we don't expose it as a property on the object
        function fbHandleStatusChange(response) {
            // Doesn't work
            otherFunction();
        }
    
        // This is also private to us
        function otherFunction() {
            alert('hello');
        }
    
        return inst;
    })();
    

    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 as controllerObject. controllerObject in the above only has one property, the function go. 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 inst anywhere in our function calls, because they’re all local to the closure scope. We can even have private data, by having other vars within the outer closure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.