PDA

View Full Version : How to define not static properties using Ext.extend?



ukostin
27 Feb 2010, 1:39 AM
For example:



var classHamster = Ext.extend(Object, {
food: '',
found: function(something){this.food = something}
});

var hamster1 = new classHamster();
var hamster2 = new classHamster();

hamster1.found('nuts');

alert(hamster2.food); // Empty, how expected.
But:


var classHamster = Ext.extend(Object, {
food: [],
found: function(something){this.food.push(something)}
});

var hamster1 = new classHamster();
var hamster2 = new classHamster();

hamster1.found('nut');
hamster1.found('nut');

alert(hamster2.food.length); // 2! This means that food is static property
How to define food: [] as not static property?

Animal
27 Feb 2010, 1:57 AM
this.food = [];

In the constructor.

?

fabio.parra
27 Feb 2010, 5:51 AM
You need to define properties that arent primitives values on constructor:


var classHamster = Ext.extend(Object, {
food: null, //its must be null on prototype because its shared
constructor : function(){
this.food = [];
},
found: function(something){
this.food.push(something)
}
});

or with same result:


var classHamster = function(){
this.food = [];
};
Ext.extend(Object,classHamster, {
food: null,
found: function(something){
this.food.push(something)
}
});

https://www.extjs.com/learn/Tutorial:Extending_Ext_for_Newbies