[Reprint] Ext. Extend understanding Javascript

Source: Internet
Author: User

Generally, if we define a class, we define a function object and write the public method to its prototype. For example:
VaR tiger = function (){}
Tiger. Prototype. Hunting = function (){}

However, to build a complete framework or class library without the help of inheritance, it is very hard and difficult to organize code. Classes in JS are function objects. To implement inheritance, you must set the prototype of the subclass as an instance of the parent class (in this way, the subclass uses all the members of the parent class prototype ), and set the constructor of the subclass prototype as the subclass itself. As shown in the following code:
Function animal (){}
Function Tiger (){}
Tiger. Prototype = new animal ()
Tiger. Prototype. constructor = Tiger

It is not difficult to implement inheritance. You can encapsulate the animal and tiger parameters above into a method (of course, you need to copy some in actual application). The Code is as follows:
Function extend (subfn, superfn ){
Subfn. Prototype = new superfn ()
Subfn. Prototype. constructor = subfn
}
As an excellent framework, ext cannot be inherited. For examplePrevious Article(Ext. Apply)Now let's get a line of code to understand Ext. Extend.
Extend: function (){
// Inline overrides
VaR IO = function (o ){
For (VAR m in O ){
This [m] = O [m];
}
};
Return function (SB, SP, overrides ){
If (typeof sp = 'object '){
Overrides = sp;
SP = Sb;
SB = function () {sp. Apply (this, arguments );};
}
VaR F = function () {}, systolic, SPP = sp. Prototype;
F. Prototype = spp;
BMI = sb. Prototype = new F ();
BMI. constructor = Sb;
SB. superclass = spp;
If (spp. constructor = object. Prototype. constructor ){
SPP. constructor = sp;
}
SB. Override = function (o ){
Ext. Override (SB, O );
};
Systolic. Override = IO;
Ext. Override (SB, overrides );
Return Sb;
};
}()

Originally, only two lines of code can implement inheritance into nearly 30 lines. What does ext do? Generally, if only two types are passed in (subfn and superfn), the above Code will be simplified
Extend: function (){
// Inline overrides
VaR IO = function (o ){
For (VAR m in O ){
This [m] = O [m];
}
};
Return function (SB, SP, overrides ){
VaR F = function () {}, systolic, SPP = sp. Prototype;
F. Prototype = spp;
BMI = sb. Prototype = new F ();
BMI. constructor = Sb;
SB. superclass = spp;
SB. Override = function (o ){
Ext. Override (SB, O );
};
Systolic. Override = IO;
Ext. Override (SB, overrides );
Return Sb;
};
}()

Define an empty function and set its prototype to SP's prototype spp. in fact, F is a substitute for SP, which can be considered as sp. Set the prototype of the sub-class sb to an instance of F, and then set its prototype constructor to its own sb. In order to facilitate the discovery of the parent class sp, A prototype spp whose superclass attribute is parent class SP is added to the subclass sb. To facilitate attribute extension, the override method for Attribute rewriting is added to the sub-class sb, the override method is also added to its prototype (so that all its instance objects can override existing attributes from an object ).

Here, I have some knowledge about inheritance (I will continue to improve my understanding in the future ). Now, with the support of inheritance, we can expand the acceleration type.

From: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 2152176

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.