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 sort of template, so then each new class, would start from scratch. 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. What's the technical explanation behind Ext JS doing this? 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 gather, this isn't how ES6 classes behave, so I'm assuming this is this due to the design of the Ext JS class system.
Here's an example.
Ext.application({
name : 'Fiddle',
launch : function() {
Ext.define('MyClass', {
extend: 'Ext.panel.Panel',
alias: 'widget.myClass',
items: [],
border: true,
myTestString: '',
constructor: function(config) {
this.items.push({
xtype: 'textfield',
fieldLabel: 'test'
});
this.myTestString += 'hi';
this.callParent(arguments);
}
});
var v = Ext.create('Ext.container.Viewport', {
defaults: {
xtype: 'myClass',
listeners: {
afterrender: function(panel) {
console.log(panel.myTestString);
}
}
},
items: [{
title: 'One'
}, {
title: 'Two'
}, {
title: 'Three'
}]
});
}
});