Extjs 之 initComponent 和 constructor的區別(轉)


在創建自定義類時,先構造(constructor)后初始化(initComponent)。如:
(在舊的Extjs 版本中使用 Ext.extend 實現擴展)

Ext.define('Btn',{
extend:'Ext.button.Button',
initComponent:function(){
alert('后初始化部件啟動...');
},
constructor:function(){
this.text = new Date();
this.renderTo = Ext.getBody();
this.callParent();
alert('先構造函數啟動...');
}
});

Ext.onReady(function(){
Ext.create('Btn');
});

initComponent是在construor里被調用,constructor是在其他地方調用;一個用於具體的創建控件,一個是用於創建控件對象


http://blog.csdn.net/oscar999/article/details/33743171

1. initComponent這個方法是在Ext.Component的構造函數(constructor)中調用的,只有直接或間接繼承自 Ext.Component的類才會在constructor里調用initComponent方法

看一下  Ext.AbstractComponent的源碼文件 src/AbstractComponent.js

在  constructor方法中調用了initComponent

2.

   1)自定義類中的 initComponent 函數中必須調用 callParent();否則 調用者無法初始化這個對象

   2)針對button 這樣的擴展組件來說,自定義類中的  constructor ,需要調用callParent( arguments);否則 調用者無法初始化這個對象

[html] view plain copy
  1. this.callParent(arguments);  
這里的arguments 是需要的。

 

(在Extjs 4 之前的版本中, 可能會看到比較多的XXX.superclass.constructor.call 寫法)



http://blog.csdn.net/alastormoody/article/details/8251018

Extjs之superclass.constructor.call(this)之理解

Ext.extend()函數提供了直接訪問父類構造函數的途徑,通過 SubClass.superclass.constructor.call(this);就可以直接調用父類的構造函數,這個函數的第一個參數總是 this,以確保父類的構造函數在子類的作用域里工作。





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM