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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:38:10+00:00 2026-06-09T12:38:10+00:00

Here is the Backbone wrapper (function(){ // Backbone.js }).call(this); For comparison. Here is the

  • 0

Here is the Backbone wrapper

(function(){
    // Backbone.js
}).call(this);

For comparison. Here is the jQuery wrapper:

(function( window, undefined ) {
    // jQuery.js
})( window );

Regarding the BackBone Wrapper:

I understand the need for a immediately invoked (via call) function expression – to provide privacy (encapsulation)

I understand why it is an anonymous function – to reduce global symbols.

However I don’t understand what ‘this’ refers to? + Why the difference in wrappers?

Reference

MDN – 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-06-09T12:38:11+00:00Added an answer on June 9, 2026 at 12:38 pm

    It helps preserving the value of this in the inner function.

    A call to an unbound function* resets this to the global object, i.e window in case of a browser.

    See this example:

    (function () {
        alert("1: " + this); // this == myObject
    
        (function () {
            alert("2: " + this); // this == window
        })();
    
        (function () {
            alert("3: " + this); // this == myObject
        }).call(this);
    }).call(myObject);
    

    *) I.e. calling func() instead of obj.func(). this would become obj in the latter case.


    First clarification:

    In case of backbone.js the reason why they wrap their code in (function () {...}).call(this) is that this way you don’t need to know the actual name of the global object (global in case of node.js).

    See http://backbonejs.org/docs/backbone.html#section-4


    Second clarification:

    If the script is run by a browser, this is the same object as window if you are not in the call context of a bound function. In a standalone JS engine (node, rhino …) the global object is not named window but global.

    The jQuery developers know you want to run their script in a browser, because you would have little fun with it if you don’t have a document to manipulate in first place.

    Backbone.js, like underscore.js, is general purpose so the developers do not know the name of the global object. And they don’t care.

    So when jQuery does

    (function (window) {
        ...
    })(window);
    

    it could have been implemented this way too

    (function () {
        var window = this;
        ...
    }).call(window);
    

    and because this===window

    (function () {
        var window = this;
        ...
    }).call(this);
    

    Read the examples backwards to see what the backbone hackers did. (They call the global object root. You may want to read the annotated code.)

    I for one would have chosen

    (function (root) {
        ...
    })(this);
    

    but in the end it does not matter I guess.

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

Sidebar

Related Questions

I have this structure of views: window.templateLoaderView = Backbone.View.extend({}); window.PopupView = templateLoaderView.extend({ initialize: function
Here is my Model View and Collection : window.Report = Backbone.Model.extend({}); window.ReportCollection = Backbone.Collection.extend({
Here is the code in a function I'm trying to revise. This example works
So here is my view: $(function() { var ImageManipulation = Backbone.View.extend({ el: $('body'), tagName:
I'm just beginning out on Backbone.js. Here's my code. $(function(){ //Backbone Model var Cat
I'm trying to run very simple view in backbone.js Here is the code: (function($){
Here's the code in CoffeeScript: class this.User extends Backbone.Model initialize: -> @access_token @cur_video_id_playing @logged_in
Here's an example of my collection view: mod.AppListView = Backbone.View.extend({ initialize: function() { var
I keep getting a filterMessages is not a function error. Here's the Backbone View
Here is my example object to demonstrate the issue. Dog = Backbone.Model.extend({ initialize: function

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.