Results 1 to 5 of 5

Thread: Pushing items onto an array in Ext JS constructor results in multiple items added

  1. #1
    Sencha User
    Join Date
    Mar 2011
    Location
    Arvada, CO
    Posts
    404
    Answers
    11

    Default Pushing items onto an array in Ext JS constructor results in multiple items added

    I have an Ext JS class that I've defined. In this class's constructor, I push a textfield onto my items array, and I add to my test string. Both the array and string are declared as empty in the class definition. However, if you try and create multiple class instances, you'll see that the items array is shared across each instance, but the string is not.

    This seems odd, as I would expect the definition of the class to be used as a template when creating each new class. It appears this is not true, so I gather this has something to do with primitives vs objects, but I'm not entirely sure why. Doesn't this in the constructor refer to that particular instance, not the actual class definition?

    I'm not asking how to fix the issue... I know how to do that (declare arrays and objects inside of initComponent/constructor). I'm asking what the technical explanation behind this is. From what I understand, this isn't how ES6 classes behave, so I'm assuming this is due to the design of the Ext JS class system? If so, will the Ext JS class system see a redesign when ES6 is fully supported?

  2. #2
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,258
    Answers
    759

    Default

    From what I understand, this isn't how ES6 classes behave
    ES6 classes don't allow you to have properties declared on a class definition like Ext does.

    There's some discussion here: http://stackoverflow.com/a/22986568/149436

    To answer why it behaves that way, there are a couple of reasons I can think of off the top of my head.

    1) Performance - Say you have an array, it would mean we would need to clone the array on every instance. What if you don't need it to be cloned? What if the contains some complex object structure:

    Code:
    foo: [{}, {}, {}, {}]
    The items inside foo could be complex object graphs with nested objects/arrays. Do they need to be cloned too? Maybe, maybe not.

    It also means we would need to track the properties that need initializing at runtime.

    2) Base Class - It would mean that classes would have to call the Ext.Base constructor so we can initialize things, which is currently not a requirement

    3) History - It's function this way for a while, it's often used to hold objects/array of things that won't change and don't need to be copied on a per instance basis.
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  3. #3
    Sencha User
    Join Date
    Mar 2011
    Location
    Arvada, CO
    Posts
    404
    Answers
    11

    Default

    Quote Originally Posted by evant View Post
    ES6 classes don't allow you to have properties declared on a class definition like Ext does.

    There's some discussion here: http://stackoverflow.com/a/22986568/149436
    Interesting read, thank you for that.

    Quote Originally Posted by evant View Post
    1) Performance - Say you have an array, it would mean we would need to clone the array on every instance. What if you don't need it to be cloned? What if the contains some complex object structure:

    Code:
    foo: [{}, {}, {}, {}]
    The items inside foo could be complex object graphs with nested objects/arrays. Do they need to be cloned too? Maybe, maybe not.

    It also means we would need to track the properties that need initializing at runtime.
    Okay, that definitely makes sense. Now, why is it that the next instance has the previous items in it? Is it because the class's definition (which is an object) is used as a template to create a new instance of that class. And because the class definition is an object with an array that had previous items pushed to it (because objects are passed by reference when creating a new class), those items appear on the next instance? Sorry for that being so wordy.

  4. #4
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,258
    Answers
    759

    Default

    If it's in the class definitions it means it will end up on the prototype. Essentially:

    Code:
    var cnt = 0;
    function Foo() {
        this.arr.push(++cnt);
    }
    
    Foo.prototype.arr = [];
    new Foo();
    console.log((new Foo()).arr);
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  5. #5
    Sencha User
    Join Date
    Mar 2011
    Location
    Arvada, CO
    Posts
    404
    Answers
    11

    Default

    Quote Originally Posted by evant View Post
    If it's in the class definitions it means it will end up on the prototype. Essentially:

    Code:
    var cnt = 0;
    function Foo() {
        this.arr.push(++cnt);
    }
    
    Foo.prototype.arr = [];
    new Foo();
    console.log((new Foo()).arr);
    Right, right, prototypical inheritance... clearly I don't deal with native JS enough. Thanks Evan.

Similar Threads

  1. tab.Panel items array - why is tabBar part of the array
    By edmeehan007 in forum Sencha Touch 2.x: Q&A
    Replies: 5
    Last Post: 17 May 2012, 3:46 PM
  2. Dynamic added form items are not in the GetForm items array
    By phpfreak in forum Ext 2.x: Help & Discussion
    Replies: 1
    Last Post: 29 Dec 2011, 11:24 AM
  3. RadioGroup in windows results in scrollbars on radio items
    By LarsBarstad in forum Ext 3.x: Help & Discussion
    Replies: 5
    Last Post: 14 Oct 2010, 12:49 PM
  4. [solved] jsonStore.query() returns results array, but items are undefined
    By admataz in forum Ext 1.x: Help & Discussion
    Replies: 3
    Last Post: 12 Oct 2007, 7:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •