PDA

View Full Version : Class properties are in someway global



tvanzoelen
28 Oct 2011, 3:33 AM
Maybe I am not understanding the Ext class system very well, but why happens the following?



Ext.onReady(function() {

Ext.define('Person', {

name: 'Terence',
pocket: new Array(),

constructor: function(config) {
this.name = config.name;
},

getName: function() {
return this.name;
}

});

var terence = new Person({ name: 'Terence' });
var craig = new Person({ name: 'Craig' });

craig.pocket.push('a lot of money');

Ext.create('Ext.panel.Panel', {
width: 400,
height: 100,
renderTo: Ext.getBody(),
html: terence.getName() + ' and ' + craig.getName() + '<br>' + 'Terence has ' + terence.pocket.pop()
});

});


I defined a Class Person. The person has a name and a pocket. Then I create two instances of the person, myself and a collegae Craig. Now the trick, my boss puts in Craigs pocket a lot of money.

like: craig.pocket.push('a lot of money');

and I take it out of my pocket: terence.pocket.pop()

Result of this magic is:

Terence and Craig
Terence has a lot of money

I think that's not fair! But if I re-initiate pocket in the constructor (this.pocket = new Array()), then every instance of Person has its own pocket. It happens only with ObjectProperties.

I expected that the properties would belong to each seperate instance and that they are not some global Class properties but instance poperties.

Now I am thinking I am not using the Ext classes in the right way. Do I?

mitchellsimoens
28 Oct 2011, 6:05 AM
Objects and Arrays are not like Strings, Ints, Floats, Bools... You need to move the pocket property within the constructor or initComponent method to create a new Array each time a new instance of Person is create otherwise they all will share the same pocket Array.

Little tip, you don't have to use 'new Array()", you can just specify '[]' to create a new Array.

tvanzoelen
28 Oct 2011, 6:27 AM
Yes I figured that out. I had put this under Discussion, because isn't it better that Ext.define would do that for you? In java, C# these properties belong to the instance of the class, so I expected the same result from Ext.define in this case.

I used the example below from the docs as a starting point.



Ext.define('My.sample.Person', {
name: 'Unknown',

constructor: function(name) {
if (name) {
this.name = name;
}

return this;
},

eat: function(foodType) {
alert(this.name + " is eating: " + foodType);

return this;
}
});


I think a programmer will think that it is possible to define your properties in the top of your class.

It would be really nice if it was possible to do, else I think this must be mentioned in the documentation.

mitchellsimoens
28 Oct 2011, 6:35 AM
When the config Object gets fully baked in it will copy that config Object so it should do this but if you don't use the config Object no it will not do this automatically for you.

tvanzoelen
28 Oct 2011, 6:45 AM
Thanks for looking into this. In my opinion it would be then a nice feature request.