constructor与initComponent

 

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

你可能感兴趣的:(function,object,ext,prototype,Class,Constructor)