首页 > 技术文章 > Extjs 之 initComponent 和 constructor的区别(转)

givemeanorange 2016-06-08 15:26 原文

在创建自定义类时,先构造(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);否则 调用者无法初始化这个对象

  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,以确保父类的构造函数在子类的作用域里工作。




推荐阅读