First, let's take a look at the typical JavaScript Inheritance Mechanism.
Define a class named baseclass, and then define two functions for baseclass: somemethod () and overridenmethod ().
VaR Baseclass = Function (){
// Do something
};
Baseclass. Prototype. somemethod = Function (){
// Do something
};
Baseclass. Prototype. overridenmethod = Function (){
// Do something
};
Using traditional methods to implement class inheritance in Javascript
VaR Subclass = Function (){
Baseclass. Call ( This );
};
Subclass. Prototype = New Baseclass ();
Subclass. Prototype. newmethod = Function (){
// Do something
};
Subclass. Prototype. overridenmethod = Function (){
// Do something
};
In the subclass constructor, you first call the baseclass constructor to initialize data, and then use
Subclass. Prototype = new baseclass ();
This statement allows the subclass class to obtain all attributes and functions in baseclass, so as to implement the inheritance operation in Javascript. It adds a new function to the subclass and overwrites the Same Name function in the parent class.
Use the Ext. Extend () function in extjs to implement the inheritance Function
VaR Subclass = Function (){
Subclass. superclass. constructor. Call ( This );
};
Ext. Extend (subclass, baseclass ,{
Newmethod: Function (){};
Overridenmethod: Function (){};
};
Ext. the extend () function provides a way to directly access the parent class constructor through subclass. superclass. constructor. call (this); you can directly call the constructor of the parent class. 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, you can directly pass the required parameters to it:
Subclass. superclass. constructor. Call (this, config); in this way, a subclass that inherits all attributes and functions of the parent class is obtained.
The Ext. Apply function is used to copy all attributes of an object to another object.
Ext. applyif is similar to Ext. apply. The difference is that if an attribute already exists in the target object, ext. applyif does not overwrite it.