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

The Archive Base Latest Questions

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

Got really confused on how JavaScript/BackboneJS works. Let’s consider the following example: window.MyView =

  • 0

Got really confused on how JavaScript/BackboneJS works. Let’s consider the following example:

window.MyView = Backbone.View.extend({
    index: 0,
    list: [],        
    initialize: function () {
        console.log("Initializing MyView");
        console.log("this.index = " + this.index);
        console.log("this.list = [" + this.list.join(',') + "]");
        this.index++;
        this.list.push(this.index);
    }
});

var first = new MyView(),
    second = new MyView();

What I was expecting here is that new MyView() will create a clean copy of window.MyView “class”, so the outbut would be:

Initializing MyView
this.index = 0
this.list = []

Initializing MyView
this.index = 0
this.list = []

But instead I’ve got

Initializing MyView
this.index = 0
this.list = []

Initializing MyView
this.index = 0
this.list = [1] // Very unexpected!

The confusing part is that integer variable “index” was in fact 0 as expected, but array “list” already contained a value from previous initialization. How come is this way?

Demo http://jsfiddle.net/fqZTp/3/

  • 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-31T02:14:15+00:00Added an answer on May 31, 2026 at 2:14 am

    The different behavior between what happens to list and index is explained well with these two answers:

    • The difference between simply reading and actually setting an object property
    • How incrementing (++) is actually an assignment behind the scenes

    To summarize them:

    • list and index both exist on MyView‘s prototype.
    • After creating your new MyView(), initialize() operates in the context of the newly created instance (i.e. not the prototype).
    • When you push() something onto list, you’re pushing it onto the list object that’s referenced by the prototype. You’re not updating the reference to the list at all, just reading it.
    • When you index++, this actually does an assignment (this.index = this.index + 1) behind the scenes. When you assign something to an object, it doesn’t matter whether or not it exists on the prototype – you are assigning it to that particular instance. The prototype does not change.

    For the behavior you’re expecting, just set both properties in your initialize function:

    window.MyView = Backbone.View.extend({
        initialize: function () {
            this.index = 0;
            this.list = [];
    
            this.index++;
            this.list.push(this.index);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been following along with this excellent asio tutorial , but have got confused
I got really confused with static and non-static functions and class properties when I
This one has really got me confused: I have a group of 'items' and
I got really confused of all the W3C specifications and implementations of different browsers.
I've got a C++ project and I'm really confused with the header organization in
Got really strange Firefox issue. Using the Microsoft __doPostBack javascript function in web forms
i got really confused every time when i encounterd bit operations,especially those shifts,rotates,overflow things
This was asked to me in an interview! i really got confused How do
Need assistance please. (More confused than anything really) I've got ASP.NET mvc controller [AcceptVerbs(HttpVerbs.Post)]
I started working with linq to SQL several weeks ago. I got really tired

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.