Code using extend (inherited by js) in Extjs _ extjs

Source: Internet
Author: User
Use the code of extend (inherited by js) in Extjs to copy & lt; a simple description of Extjs & gt; Note: Copy <深入浅出extjs> Books
The traditional js implementation inheritance operation is:
1. Define a parent class

The Code is as follows:


Var BaseClass = function (){
//.....
};
BaseClass. prototype. someMethod = function (){
//.....
};
BaseClass. prototype. overridenMethod = function (){
//....
}


Define two someMethod and overridenMethod functions for BaseClass, and then define a subClass. You can directly inherit all attributes and functions from BaseClass,

The Code is as follows:


Var subClass = function (){
BaseClass. call (this );
};
SubClass. prototype = new BaseClass ();
SubClass. prototype. newMethod = function (){
//...
};
SubClass. prototype. overridenMethod = function (){
//...
}


In the above Code, The subClass constructor first calls the BaseClass constructor to initialize data, and then uses subClass. prototype = new BaseClass (); this statement allows the subClass class to obtain all attributes and functions in BaseClass. In this way, inheritance is realized. After that, we can operate the prototype of subClass, add a new function to the subClass, or overwrite the Same Name function of the parent class.
In EXT, use the Ext. extend () function to implement the inheritance function:

The Code is as follows:


Var subClass = function (){
SubClass. superclass. costructor. call (this );
};
Ext. extend (subClass, BaseClass ,{
NewMethod: function (){
//...
},
OverridenMethod: function (){
//....
}
});


The Ext. extend () function can directly call the constructor of the parent class through subClass. superclass. costructor. call (this. The first parameter of this function is always this to ensure that the constructor of the parent class works in the scope of the subclass.
If the constructor of the parent class needs to input parameters, we can directly pass the required parameters to the constructor, for example:
SubClass. superclass. costructor. call (this, config );
In this way, we get a subclass that inherits all the attributes and functions of the parent class.
Related Article

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.