当我应该使用initComponent
比较constructor
?
我一直在使用initComponent来扩展我的对象,但查看Ext.define的文档是使用构造函数来查看它们然后放置.有什么不同?
相比:
Ext.define('My.app.PanelPart2', {
extend: 'My.app.Panel',
constructor: function (config) {
this.callSuper(arguments); // calls My.app.Panel's constructor
//...
}
});
Run Code Online (Sandbox Code Playgroud)
至
Ext.define('My.app.PanelPart2', {
extend: 'My.app.Panel',
initComponent: function (config) {
Ext.apply(this, config);
this.callParent(arguments);
}
});
Run Code Online (Sandbox Code Playgroud)
我知道有些组件没有初始化(我正在看着你Ext.data.Store
),这导致我只倾向于编写构造函数,因为这应该是通用的.
小智 36
constructor
是初始化对象实例的OOP标准,并且在每个ExtJS类(由其创建的所有内容Ext.define
)中都可用.
initComponent
是用于初始化组件的ExtJS扩展 - 扩展的类Ext.Component
.它使用模板化方法模式,并在自定义组件init之前和之后启用一些标准初始化步骤.像这样的东西:
Ext.Component.constructor() {
- init events
- apply config
- create plugins
- ...
- your custom component initialization (initComponent)
- ...
- init plugins
- etc.
}
Run Code Online (Sandbox Code Playgroud)
我最好的实践是initComponent
在我创建自定义组件时使用,并且constructor
仅用于泛型类.