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), butbefore 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.
基本知識:object沒有prototype;但object有 constructor;constructor是function,所以constructor有prototype。那麼原型繼承的核心是什麼呢?就 是把object的constructor的prototype的屬性複製到了此object上。
當然,實際情況要複雜得多,這裡提到這些,是因為和Ext的一個方法有關:Ext.extend(parent, config);
其中值得關注的是config。例如這麼寫:
Ext.ns('Demo','Demo.Configuration');
Demo.Configuration.Sample1 = Ext.extend(Ext.Component, {
name : '李東東',
otherConfig : {}
initComponent : function() {
otherConfig = otherConfig || {sex: 'female'};
}
});
上面的name和otherConfig屬性,在new完第一個Demo.Configuration.Sample1對象之後,就會出現在 Demo.Configuration.Sample1.constructor.prototype的屬性之中。也就是說,在new 完第一個對象之後,再new其他的對象時,將會在初始時就自動帶了和第一個對象一模一樣的屬性名稱值對,這個例子裡是{sex : 'female'},注意是在最初始的狀態就是這個值,而不再是想當然的到initComponent中才改變的{}了。例子中給otherConfig
賦值是寫死的,但實際情況很可能是根據不同的條件動態賦值,這時候這個特性如果不留意,就會很坑人。
轉載http://www.cnblogs.com/damnedmoon/archive/2010/05/25/1743706.htm