[Ext JS 4] Extjs 它 initComponent 和 constructor差分

initComponent 和 constructor是什么

Extjs 提供的组件还是挺丰富的, 可是有时候需求更丰富。

当Extjs 原生的组件无法实现我们的要求的时候, 就须要扩展Extjs 的组件实现自制组件了。

除了这种使用状况, 有时候对于一些同样却有使用非常多的配置, 可能像把它独立出来,单独设为一种组件供大家调用, 节省开发时间和提高代码重用度。

initComponent 和 constructor 就是Extjs 提供用来实现继承和扩展的方式。


Ext.define 实现扩展

在Extjs 中使用Ext.define来实现扩展, initComponent 和 constructor的使用方式相似

	Ext.define('Ext.oscar999.button.MyButton', {
		extend : 'Ext.button.Button',
		initComponent : function() {
			//do something
		},
		constructor : function() {
			//do something
		}
	});

一般状况上,加上 xtype 的定义, 相似:


(在旧的Extjs 版本号中使用 Ext.extend 实现扩展)


那么这两种使用方法到底该怎样使用? 两者的使用又有什么差别呢?

initComponent 和 constructor差别于联系

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);否则 调用者无法初始化这个对象

this.callParent(arguments);
这里的arguments 是须要的。


(在Extjs 4 之前的版本号中。 可能会看到比較多的XXX.superclass.constructor.call 写法)

sencha 的官网中有一篇针对这两个差别的讨论:

http://www.sencha.com/forum/showthread.php?47210-constructor-Vs-initComponent

只是语法是基于Extjs 3 来讨论的。 笔者认为作用不是非常大。


就笔者实际的开发经验来看, 基本上使用initComponent 这是可能实现的开发的要求。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值