No parameters and parameter class inheritance in Javascript _ javascript skills

Source: Internet
Author: User
This article describes how to solve the problem of no-parameter and parameter-class inheritance in Javascript. This article explains the problem of non-parameter class inheritance and the problem of parameter-class inheritance, and provides a solution, for more information about Javascript class inheritance, the prototype chain is inseparable, but inheritance implemented only through prototype chain has many defects.

No parameter class inheritance issues

Let's take A look at the sample code to implement B inherited from:

The code is as follows:


Function (){
}
A. prototype. a1 = function (){};

Function B (){
}
B. prototype = new ();
B. prototype. b1 = function (){};

Var B = new B ();
Alert (B. constructor = A); // true
Alert (B. constructor = B); // false


The main problems with this code are:

1. you need to instantiate A as the prototype of B and then execute the constructor of. However, according to the object-oriented rules, the constructor of B and its parent class A should not be executed before B is instantiated.

2. changed prototype of B, resulting in B. constructor not B but.

Questions about inheritance of parameters

Assume that A and B both have two string parameters s1 and s2. A calculates the total length of the Two Strings. B calls A directly with s1 and s2 as the parameters:

The code is as follows:


Function A (s1, s2 ){
This. totalLength = s1.length + s2.length;
}
A. prototype. a1 = function (){
};

Function B (s1, s2 ){
}
B. prototype = new ();
B. prototype. b1 = function (){
};

New B ("AB", "123 ");


We can see that there is no way to upload s1 and s2 to A in this code, but there is no parameter when instantiating A as A prototype of B, so an exception occurs:

The code is as follows:


S1 is undefined


Solution

The s1 and s2 scopes are only within B. to pass them to A, they can only be operated in B. This can be achieved through the apply method of the function:

The code is as follows:


Function B (s1, s2 ){
A. apply (this, arguments );
Alert (this. totalLength );
}


The next question is how to add the method to the prototype of B. This is not difficult. just traverse A. prototype and copy the method to B. prototype. It should be noted that for methods with the same name, the subclass takes precedence over (overload), and therefore cannot overwrite:

The code is as follows:


For (var m in A. prototype ){
If (! B. prototype [m]) {// the parent class cannot overwrite the subclass method.
B. prototype [m] = A. prototype [m];
}
}


Postscript

Considering that C #, Java and other advanced languages all discard multi-inheritance, this article only discusses the single inheritance situation. The inheritance method described in this article will also be written as an extension of jRaiser, which will be released later.

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.