[Ext JS 4] differences between initComponent and constructor in Extjs

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.