Note: Copied from the book
The traditional js implementation inheritance operation is:
1: Define a parent class
var BaseClass = function(){
//.....
};
BaseClass.prototype.someMethod = function( ){
//....
};
BaseClass.prototype.overridenMethod = function(){
//....
}
Define two functions someMethod and overridenMethod for BaseClass, and then define a subClass subclass that can directly inherit all properties and functions from BaseClass.
var subClass = function(){
BaseClass.call(this);
};
subClass.prototype = new BaseClass();
subClass.prototype.newMethod = function(){
//...
};
subClass.prototype.overridenMethod = function(){
//.. .
}
In the above code, the constructor of subClass first calls the constructor of BaseClass to initialize the data, and then uses the statement subClass.prototype = new BaseClass(); to make the subClass class Get all properties and functions in BaseClass. In this way inheritance is achieved. After this, we can operate the prototype of subClass, add new functions to the subclass or override the same-name function of the parent class.
How to use the Ext.extend() function in EXT to implement the inheritance function:
var subClass = function(){
subClass.superclass.costructor.call(this);
};
Ext.extend(subClass, BaseClass,{
newMethod: function(){
//...
},
overridenMethod : function(){
//....
}
});
In the Ext.extend() function, you 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 parent class's constructor works in the scope of the child class.
If the constructor of the parent class needs to pass in parameters, we can pass the required parameters directly to it, such as:
subClass.superclass.costructor.call(this, config);
This way We get a subclass that inherits all the properties and functions of the parent class.