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.