No parameters of class hierarchy problems

look at A sample code, implementation inheritance in A: B

the function A () {
}
Amy polumbo rototype. A1=function () {
};

the function B () {
}
p. rototype=new A ();
p. rototype. B1=function () {
};

var b=new b ();
alert (biggest onstructor==A);//true
alert (biggest onstructor==B);//false

the main problem with this code is:

* need to instantiate A as B prototype, at this time to perform A constructor. But according to the object-oriented rules, instantiated before B, B and its superclass constructor should not be executed.
* change the prototype of B, cause the biggest onstructor A rather than B.

of class hierarchy problems do.

assume that A and B are parameters of the two strings s1 and s2, calculated the total length of two pieces of string in A, B directly to s1, s2, calls for A parameter A:

the function A (s1, s2) {
this. TotalLength=s1. The length + s2. Length;

}. Amy polumbo rototype a1=function () {
};

the function B (s1, s2) {
}
p. rototype=new A ();
p. rototype. B1=function () {
};

new B (" ab ", "123");

you can see, there is no way to this code to A, s1 and s2 and because instantiating A as B prototype without parameters, so the exception:

s1 is undefined

solution

s1 and s2 scope within the B, only to put them to A, can only operate in B, with the aid of the apply method can realize the function:

the function B (s1, s2) {
A.a pply (this, the arguments);
alert (enclosing totalLength);
}

the next question is how to add A method to B's prototype. It is not difficult, as long as the traversal Amy polumbo rototype, copy method to the p. rototype can. Note that for the same name, the method of natural subclass is preferred (overloading), which could cover:

the for (var m in Amy polumbo rototype) {
if (! P. rototype [m]) {//parent can't cover the subclass methods
p. rototype [m]=Amy polumbo rototype [m].

}}

postscript

considering the higher language such as c #, Java has abandoned the multiple inheritance, therefore, this article discussed is only a single inheritance. And inherit the methods described in this article, also can write jRaiser an extension, release later.


This concludes the body part