constructor與initComponent

來源:互聯網
上載者:User

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.