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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:28:47+00:00 2026-06-13T16:28:47+00:00

I’m just getting into Backbone, and one thing that I don’t understand is why

  • 0

I’m just getting into Backbone, and one thing that I don’t understand is why the ‘on()’ method for models always takes three arguments–event, handler, and context.

It seems that almost always ‘this’ is used for context and I haven’t seen any other usage. Even if there were, since I haven’t seen one yet it must be pretty rare.

So my question is: When does one use a context other than ‘this’, and why is Backbone designed this way? By the way, I do understand why you need to provide context, it’s just that I wonder why the method syntax specifies that I use three arguments instead of making the last argument optional–which seems to be always ‘this’ and feels redundant. I’m sure I’m missing something. Please someone help me understand. Thank you!

  • [EDIT] Why can’t one do something like:

    model.on = function(event, callback){
      model.on_with_three_args.call(this, event, callback, this);
    });
    
    model.on_with_three_args = function(event, callback){
      /* whatever the on() is supposed to do */
    });
    
  • 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-06-13T16:28:48+00:00Added an answer on June 13, 2026 at 4:28 pm

    Suppose we’re in a view that’s based on a model and we want to bind to the model’s change event:

    this.model.on('change', this.render);
    

    The on call sees two things:

    1. The event name, a simple string.
    2. The handler, a function.

    on has no way of knowing what this means in this.render, it just sees a function; on won’t even know the difference between the call above and this:

    this.model.on('change', function() { ... });
    

    If your function needs a particular context then you have two choices:

    1. Create a bound function using _.bind, _.bindAll, Function.bind, $.proxy, CoffeeScripts =>, the var _this = this closure trick, or any of the ways of creating or simulating a bound function.
    2. Tell it which context you want by saying:

      this.model.on('change', this.render, this);
      

    There’s no way to unroll the call stack to see which this you want so you have to be explicit about it.

    Backbone will call the callback like this:

    node.callback.apply(node.context || this, ...);
    

    where node.callback is the callback function and node.context is the third argument (if any) given to on. If you don’t specify the context then you’ll get whatever this happens to be when trigger is called; in the example above, this would end up being the model.

    So the third argument to on actually is optional but the default value isn’t terribly useful and there is no way to choose a better default, the information you need to choose a sensible context simply isn’t accessible in JavaScript. This is why you see so much _.bindAll(this, ...) boilerplate in Backbone views.


    If you tried something like this:

    model.on = function(event, callback){
        model.on_with_three_args.call(this, event, callback, this);
    });
    

    then this in that context would usually be model so you’d really be saying:

    model.on = function(event, callback){
        model.on_with_three_args.call(model, event, callback, model);
    });
    

    or

    model.on = function(event, callback){
        model.on_with_three_args(event, callback, model);
    });
    

    and there’s little point to any of that. The value of this inside on has little if anything to do with the value of this in the code that calls on. this in JavaScript is not a variable, it is a keyword which refers to the current calling context.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

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.