What is initComponent and constructor?
The components provided by Extjs are quite rich, but sometimes the demand is richer.
When the native components of Extjs cannot meet our requirements, we need to extend the components of Extjs to implement self-made components.
In addition to this situation, sometimes many identical configurations are used, which may be set as a separate component for everyone to call, save development time and improve code reuse.
InitComponent and constructor are the methods provided by Extjs for inheritance and extension.
Ext. define implementation Extension
Ext. define is used in Extjs for extension. The use of initComponent and constructor is similar.
Ext.define('Ext.oscar999.button.MyButton', {extend : 'Ext.button.Button',initComponent : function() {//do something},constructor : function() {//do something}});
In general, the definition of xtype is added, which is similar:
(Use Ext. extend in the old Extjs version for extension)
How can we use these two methods? What is the difference between the two?
InitComponent and constructor are different from each other
1. The initComponent method is called in the constructor of Ext. Component. Only classes that inherit directly or indirectly from Ext. Component can call the initComponent method in constructor.
Let's take a look at the Ext. AbstractComponent source code file src/AbstractComponent. js
InitComponent is called in the constructor method.
2.
1) callParent () must be called in the initComponent function of the custom class; otherwise, the caller Cannot initialize this object.
2) for extended components such as button, callParent (arguments) must be called for constructor in the Custom class; otherwise, the caller Cannot initialize this object.
this.callParent(arguments);
The arguments here is required.
(In versions earlier than Extjs 4, you may see many XXX. superclass. constructor. call statements)
Sencha's official website has a discussion about the two differences:
Http://www.sencha.com/forum/showthread.php? 47210-constructor-Vs-initComponent
However, the syntax is discussed based on Extjs 3. I don't think it works very well.
Based on my actual development experience, basically using initComponent can meet the development requirements.