PDA

View Full Version : Why not use this.constructor.superclass ?



Tom23
5 Nov 2009, 10:47 AM
I was wondering, is it safe to use


MyComponent = Ext.extend(fooClass, {
initComponent: {
// ...
this.constructor.superclass.initComponent.apply(this, arguments);
}
})instead of

MyComponent = Ext.extend(fooClass, {
initComponent: {
// ...
MyComponent.superclass.initComponent.apply(this, arguments);
}
})? When might this cause problems?

(Obviously, this.constructor is more convenient when it comes to renaming MyComponent.)

Animal
5 Nov 2009, 10:53 AM
Yes, you should be fine to use that. I use that in generated initComponent methods where the code generator doesn't know or care what the class is.

Tom23
13 Dec 2009, 1:14 AM
Oh no! I just ran into an infinite loop. :( Problem is:



MyComponent = Ext.extend(fooClass, {
initComponent: function () {
this.constructor.superclass.initComponent.apply(this, arguments);
}
});

MyChildComponent = Ext.extend(MyComponent, {/*foo*/});

instance = new MyChildComponent();


instance.initComponent will call MyComponent.prototype.initComponent, which will call itself over and over again, because this.constructor is MyChildComponent, even in MyComponent.

So, don't use this.constructor.superclass if you want to extend...