initComponent在ext4中的作用


[size=medium]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), but before 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.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值