View Full Version : Classsystem config with array -> 2 instances have the same array-object

23 Oct 2012, 2:57 AM
Pretty simple example:

I created a class with an array as config object (empty by default). Afterwards i instantiate 2 objects of this class with Ext.create.
If i add an item to the array-object of 1 of the instances the array of the second one is also altered. This means that the array of both instances seems to be the same object.

Working example: http://jsfiddle.net/SmQJv/1/

Ext.define('Class', {
config: {
arr: []

constructor: function(config) {

return this;

Ext.onReady(function() {
var instance1 = Ext.create('Class'),
instance2 = Ext.create('Class'),
ta = Ext.get('textarea');

// output values before
ta.dom.value = 'before:';
ta.dom.value += '\r\ninstance1 arr: ' + instance1.arr;
ta.dom.value += '\r\ninstance2 arr: ' + instance2.arr;

// add new item to array of instance1 and change it's index
ta.dom.value += '\r\n\r\nAdd item to array of instance1 only...';

// output values after
ta.dom.value += '\r\n\r\nafter:';
ta.dom.value += '\r\ninstance1 arr: ' + instance1.arr;
ta.dom.value += '\r\ninstance2 arr: ' + instance2.arr;

ta.dom.value += '\r\n\r\nWhy do both instances have the same array-object?';

If i pass an config-object to the create-method (e.g. instance2 = Ext.create('Class', { arr: [] })) everything seems to be fine and both instances have a different array-object.

I would really appreciate if anyone could tell me what is happening here and how i could prevent it.


edit: Simplified example a little bit..

24 Oct 2012, 7:28 AM
Use the built-in setter/getter that is created when using the config object:


instance1 arr: 3
instance2 arr:

To clarify, your approach is setting it at the base level.