Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The different behavior between what happens to <code>list</code> and <code>index</code> is explained well with these two answers:</p> <ul> <li><a href="https://stackoverflow.com/a/1534286/29995">The difference between simply reading and actually setting an object property</a></li> <li><a href="https://stackoverflow.com/a/261948/29995">How incrementing (++) is actually an assignment behind the scenes</a></li> </ul> <p>To summarize them:</p> <ul> <li><code>list</code> and <code>index</code> both exist on <code>MyView</code>'s prototype.</li> <li>After creating your <code>new MyView()</code>, <code>initialize()</code> operates in the context of the newly created instance (i.e. not the prototype).</li> <li>When you <code>push()</code> something onto <code>list</code>, you're pushing it onto the list object that's <strong>referenced</strong> by the prototype. You're not updating the reference to the list at all, just reading it.</li> <li>When you <code>index++</code>, this actually does an <strong>assignment</strong> (<code>this.index = this.index + 1</code>) behind the scenes. When you assign something to an object, it doesn't matter whether or not it exists on the prototype - you are <strong>assigning it to that particular instance</strong>. The prototype does not change.</li> </ul> <hr/> <p>For the behavior you're expecting, just set both properties in your <code>initialize</code> function:</p> <pre><code>window.MyView = Backbone.View.extend({ initialize: function () { this.index = 0; this.list = []; this.index++; this.list.push(this.index); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload