Tuesday, July 28, 2009

Objects extended in ExtJS

If you try to extend Object with ExtJS and add some properties to the new class:
1 MyClass = Ext.extend(Object, {
2 p: 1
3 }

When you will create a new instance of this class, you loose all your properties previously added.
1 var obj = new MyClass ();
2 console.log(obj.p); // will be 'undefined'

This is the issue:
1 MyClass = Ext.extend(Object, {
2 constructor : function(options){
3 Ext.apply(this, options || {} );
4 },
5 p: 1
6 });

And to extend again:
1 MySecondClass = Ext.extend(MyClass, {
2 constructor : function(options){
3 //call the base constructor
4 MyClass.superclass.constructor.call(this, options);
5 this.r = this.q + this.p;
6 },
7 q: 2
8 });

No comments: