Results 1 to 5 of 5

Thread: States of object is not maintained when multiple instances of a class are created

    Looks like we can't reproduce the issue or there's a problem in the test case provided.
  1. #1
    Ext JS Premium Member
    Join Date
    Nov 2007
    Location
    New Hampshire/New England - USA
    Posts
    42
    Vote Rating
    0
      0  

    Default States of object is not maintained when multiple instances of a class are created

    REQUIRED INFORMATION


    Ext version tested:
    • Ext 4.1.1


    Browser versions tested against:
    • Any


    DOCTYPE tested against:
    • n/a


    Description:
    • I ran into an issue that took quite some time to debug in our application. I have isolated it to the fact that when one creates 2 instances of a class the state of the first instance is lost and replaced by the state of the second one. This is happening when the class is defined by passing a function instead of an object as the second parameter to Ext.define as documented in extjs-4.1.1/docs/index.html#!/api/Ext-method-define

      I have reproduced it with the simple class below


    Steps to reproduce the problem:
    • try code below
    • you will see that when the second instance is created the name property in the first instance takes the value of the one in the second instance


    The result that was expected:
    • each instance to have its own state


    Test Case:

    Code:
    Ext.define('test.class.PrivateExample',function(){
     var itsName = 'defaultName';  // this is private
    
    function constructor(a) { if ( a !== undefined && a.name !== undefined ) {itsName = a.name;}}
    
    function privateFct1() {return itsName;}
    
    // The public API to this class
    return  { publicFunction1: privateFct1, constructor: constructor	}
    });
    
    
    function testPrivateExample() {
    var testObject = Ext.create('test.class.PrivateExample',{name: 'test1'});
    console.log ( "First Object: name: " + testObject.publicFunction1() );
    var testObject2 = Ext.create('test.class.PrivateExample',{name: 'test2'});
    console.log ( "Second Instance: name: " + testObject2.publicFunction1() );
    console.log ("  Now you see that first instance name is incorrect ! ");
    console.log ( "First Instance: name: " + testObject.publicFunction1() );
    }


    HELPFUL INFORMATION


    Screenshot or Video:
    • attached


    See this URL for live test case: http://


    Debugging already done:
    • none


    Possible fix:
    • not provided


    Additional CSS used:
    • only default ext-all.css
    • custom css (include details)


    Operating System:
    • ________
    • any

  2. #2
    Sencha User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,060
    Vote Rating
    1389
      0  

    Default

    The function is executed when the class is defined. If you didn't create any instances you will be able to see the function is executed, so the itsName variable is then shared.
    Mitchell Simoens @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

  3. #3
    Sencha User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,002
    Vote Rating
    936
      0  

    Default

    This isn't a bug, you've just created a closure.

    It's similar to doing this:

    Code:
    (function(){
        var name = 'foo';
        
        Ext.define('MyClass', {
            
            constructor: function(cfgName) {
                name = cfgName;
            },
            
            fn: function(){
                return name;
            }    
        });
    })();
    
    Ext.onReady(function() {
        var o1 = new MyClass('foo'),
            o2 = new MyClass('bar');
            
        console.log(o1.fn(), o2.fn());
    });
    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.

  4. #4
    Ext JS Premium Member
    Join Date
    Nov 2007
    Location
    New Hampshire/New England - USA
    Posts
    42
    Vote Rating
    0
      0  

    Question hold on...

    Mitchell,

    If it is not a bug, here are some additional questions/comments:

    1) I would think the doc needs to be made clearer (http://docs.sencha.com/ext-js/4-1/#!...-method-define). In particular, the example provided gives the impression that passing a function is equivalent to passing an object literal but simply provides a mechanism to make properties/functions private.

    2) Then, what is the official/supported technique to have private properties/functions?

    thanks.

  5. #5
    Sencha User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,002
    Vote Rating
    936
      0  

    Default

    1) This is a fairly common JS pattern, it's not suggesting that they are "private" as in instance variables, but "private" as in local to that function scope (it's a closure).

    2) If you absolutely ~must~ have private members, you could do something like this (not recommended):

    Code:
    Ext.define('MyClass', {
        constructor: function(cfgName){
            var private = 0,
                name = cfgName;
            
            this.getPrivate = function(){
                return ++private;    
            };
            
            this.getName = function(){
                return name;
            };
        }
    });
    
    Ext.onReady(function(){
        var o1 = new MyClass('Foo'),
            o2 = new MyClass('Bar');
            
        console.log(o1.getPrivate(), o1.getName(), o1.private, o1.name);
        console.log(o2.getPrivate(), o2.getName(), o2.private, o2.name);
    });
    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.

Posting Permissions

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