Home > Web Front-end > JS Tutorial > Code for using extend (js inheritance) in Extjs_extjs

Code for using extend (js inheritance) in Extjs_extjs

WBOY
Release: 2016-05-16 17:55:17
Original
1102 people have browsed it

Note: Copied from the book
The traditional js implementation inheritance operation is:
1: Define a parent class

Copy code The code is as follows:

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.
Copy code 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 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:
Copy the code The code is as follows:

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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template