Hybrid View
-
19 Oct 2012 12:58 PM #1Ext JS Premium Member
- Join Date
- Nov 2007
- Location
- New Hampshire/New England - USA
- Posts
- 42
- Vote Rating
- 0
States of object is not maintained when multiple instances of a class are created
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
-
20 Oct 2012 6:17 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,624
- Vote Rating
- 435
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 @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
21 Oct 2012 6:33 PM #3
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()); });Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
22 Oct 2012 5:28 AM #4Ext JS Premium Member
- Join Date
- Nov 2007
- Location
- New Hampshire/New England - USA
- Posts
- 42
- Vote Rating
- 0
hold on...
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.
-
22 Oct 2012 2:50 PM #5
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); });Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
Looks like we can't reproduce the issue or there's a problem in the test case provided.


Reply With Quote