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 7093773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:28:52+00:00 2026-05-28T08:28:52+00:00

Background: I am building a webapp that makes use of JQuery and the Facebook

  • 0

Background: I am building a webapp that makes use of JQuery and the Facebook Javascript API. Since some users don’t like Facebook, I have also built a thunking layer that emulates the necessary identity APIs for users who prefer not to use Facebook. To keep things clean and avoid code duplication, I have organized my Javascript code into a few classes.

Both Facebook and JQuery APIs make extensive use of callbacks. Since all of my functions are bound into objects, I found that I am using the following pattern frequently:

var obj_ref = this;
some_jquery_function_that_takes_a_callback(/* various args */,
    function() { obj_ref.my_callback_method(); });

For readability, the obj in obj_ref is actually the name of the class.

Is there some nifty Javascript magic that is simpler or clearer, or is this as good as it gets?

Update: Some good commentary so far in the answers. I should have added that my callback methods generally needs to refer to variables or functions bound to the class. If I don’t need that, then the anonymous function wrapper is unnecessary.

Update2: I’ve selected a best answer, but you should carefully read all of the answers. Each provides some useful insight into possible solutions.

  • 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-28T08:28:53+00:00Added an answer on May 28, 2026 at 8:28 am

    If you need your this to be your obj_ref and you can assume an update to date JavaScript (which sadly you probably can’t), you could use bind to do away with the wrappers:

    Creates a new function that, when called, itself calls this function in the context of the provided this value, with a given sequence of arguments preceding any provided when the new function was called.

    Then you could bind your methods to your objects and this:

    some_jquery_function_that_takes_a_callback(/* various args */,
        function() { obj_ref.my_callback_method(); });
    

    would be the same as:

    // In some initialization pass...
    obj_ref.my_callback_method = obj_ref.my_callback_method.bind(obj_ref);
    
    // And later...
    some_jquery_function_that_takes_a_callback(/* various args */,
        obj_ref.my_callback_method);
    

    Then this would be obj_ref when my_callback_method is called.

    Alternatively, you could pull in Underscore.js and use its bind or bindAll. You could always grab just bind or bindAll out of Underscore.js if you didn’t want the whole thing.

    Or, since you have jQuery in play already, you could use $.proxy in place of the standard bind:

    Takes a function and returns a new one that will always have a particular context.

    So you could do it like this:

    // In some initialization pass...
    obj_ref.my_callback_method = $.proxy(obj_ref.my_callback_method.bind, obj_ref);
    
    // And later...
    some_jquery_function_that_takes_a_callback(/* various args */,
        obj_ref.my_callback_method);
    

    Thanks to dsfq for reminding me about jQuery’s version of bind.

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

Sidebar

Related Questions

Background: I am building a C# Forms application that works with the Facebook API.
I'm building a background job that's updating users' statistics for a web application. The
Some Quick Background I'm building an iOS app that uses html web views in
Some background I'm currently building a predominantly classic web app (rather than a single
Background: We're building an application that allows our customers to supply data in a
Background: I'm building a small application that will be run daily, pulling data from
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
I'm building a webapp for Android smartphones that runs with the OS internet browser.
Background I'm working on building an ASP.Net MVC 3 application that utilizes: JQueryUI for
Right now I'm building a project management app in rails, here is some background

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.