views:

533

answers:

2

In extjs you can always extend an extjs class via the constructor(). For classes derinving from Component you can also extend via initComponent().

I am wondering why so many code extend via initComponent, whereas constructor seems to be the universal extension method. Does initComponent offer clear advantage over constructor?

+3  A: 

First off, the ability to override via constructor was added in a later version of Ext than initComponent, so all code of a certain age would have to use initComponent. These days, you would still override initComponent if you want to do anything after the base class initComponent is called (constructor would be too early for this), but before the component is rendered. In many cases (like the most common, setting up configs), it does not practically matter either way and most people do whatever is most convenient. However, there are some cases where it matters.

bmoeskau
Ok, I will default to use constructor as extension method. When I need to overide via initComponent it is a sign that I am (too much) intimate with the class' internals.
Exception e
A: 

The two methods are compared here:

http://www.uvla.net/2010/01/extjs-how-to-extends-a-component/

Christiaan
That link doesn't have a comparison of the two methods, it's only the same thing done both ways, without an explanation of why you'd do it either way.
Juan Mendes