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

  • Home
  • SEARCH
  • 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 7668467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:22:14+00:00 2026-05-31T15:22:14+00:00

I was wondering what is the intended method for dealing with this references in

  • 0

I was wondering what is the intended method for dealing with “this” references in callbacks frequently used in frameworks like jQuery and Backbone.js

Here is the simplified example I’m having a problem with, using Backbone.js and jQuery UI, but it is not really specific to these frameworks.

var MyView = Backbone.View.extend({
   render: function() {
      $('#mybutton').click(function() {
         // My code in here
      });
   },

   callMe: function() {
      alert('Rawr!');
   }
});

Now, how would I go about referencing the instance of MyView from inside the click handler? For example, how would I call “callMe” from inside the click handler? Outside the handler I’d just call this.callMe(); but “this” gets replaced with the DOM element inside the click handler. What is the best way to approach this?

  • 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-31T15:22:16+00:00Added an answer on May 31, 2026 at 3:22 pm

    The most effective way is simply to store the result of this in another variable, and then use that within the callback;

    var MyView = Backbone.View.extend({
       render: function() {
          var that = this;
    
          $('#mybutton').click(function() {
              // use that for MyView and this for the DOM element
          });
       },
    
       callMe: function() {
          alert('Rawr!');
       }
    });
    

    Alternately, you can use either jQuery.proxy() or the HTML 5 (beware of backwards compatability) Function.prototype.bind() method to define the value of this. Obviously then you’d loose the ability to use this to refer to the DOM element, so would have to rely on the target property of the Event object;

    var MyView = Backbone.View.extend({
       render: function() {
          $('#mybutton').click((function(e) {
              // Use `this` for MyView and `e.target` for the DOM element
          }).bind(this));
       },
    
       callMe: function() {
          alert('Rawr!');
       }
    });
    

    Or:

    var MyView = Backbone.View.extend({
       render: function() {
          $('#mybutton').click(jQuery.proxy(function(e) {
              // Use `this` for MyView and `e.target` for the DOM element
          }, this));
       },
    
       callMe: function() {
          alert('Rawr!');
       }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to remove all unchecked warnings from this general utility method (part of
This question title is a bit longer than what I intended, but I'm wondering
Wondering if anyone can help me with this annoying but trivial (in terms of
Wondering if this is possible. We have an 3rd Party library that contains identification
I'm wondering it this is a really bad usability idea, so I'm open to
It seems the render method has changed. In the view I used to be
This article claims that each register has an intended purpose and more importantly, When
Possible Duplicate: When should I make explicit use of the this pointer? I'm wondering
I am wondering - What's the most effective way of parsing something like: {{HEADER}}
This question skirts around what I'm wondering, but the answers don't exactly address it.

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.