Base. js -- Inherited implementation ==========================
[Note]: After inheritance, if the parent class is a class, it will inherit its attributes, methods (including declared using prototype), and static methods. Otherwise, only attributes and methods are available.
Copy codeThe Code is as follows:
Object. prototype. extendf = function (a, B ){
If (! A |! B) return;
Var fa = typeof a = "function ";
Var fb = typeof B = "function ";
Var cha = function (a, B ){
For (var c in B ){
If (a [c] = undefined) // rewrite the subclass
A [c] = B [c];
}
Return a; // return the inherited object
}
If (fa & fb ){
B. apply (this, a. arguments );
Cha (a, B );
This ["base"] = new B; // access the parent class through base
Return cha (this, B. prototype );
}
Else if (! Fa & fb ){
Cha (a, new B );
A ["base"] = new B;
Return cha (a, B );
} Else if (fa &&! Fb ){
Cha (a, B );
This ["base"] = B;
Return cha (this, B );
} Else if (! Fa &&! Fb ){
A ["base"] = B;
Return cha (a, B );
}
}
Test page: Usage
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript" src = "base. js"> </script>
<Script type = "text/javascript">
Var car2 = {
Name: "car [parent class ]",
Price: "tens of thousands [parent class ]",
Start: function (){
Alert (this. name + "started 2! [Parent class ]");
},
Run: function (){
Alert (this. name + "2 while driving... [Parent class ]");
},
Stop: function (){
Alert (this. name + "stopped 2! [Parent class ]");
},
Remark: function () {return "[parent class] 2 I am a car" + this. name + "; Value" + this. price ;}
// This. remark = "I am a car" + this. name + "; Value" + this. price;
}
// Car2.prototype. extra = function (ext ){
// Return this. name + "the tariff 2 is:" + ext;
//}
Car2.protect = "[parent class] 2 protected ";
Car2.noExtra = function (){
Return car. protect + "do not pay duty 2 [parent class ]";
}
Var car = function (name, price ){
This. name = name | "car [parent class]";
This. price = price | "tens of thousands [parent class]";
This. start = function (){
Alert (this. name + "started! [Parent class] ");
};
This. run = function (){
Alert (this. name + "is driving... [Parent class] ");
};
This. stop = function (){
Alert (this. name + "stopped! [Parent class] ");
};
This. remark = function () {return "[parent class] I am a" + this. name + "; Value" + this. price ;};
// This. remark = "I am a car" + this. name + "; Value" + this. price; // note: in this case, parameter name and price will not be passed, so comments
}
Car. prototype. extra = function (ext ){
Return this. name + "the tariff is [parent class]:" + ext;
}
Car. protect = "[parent class] protected ";
Car. noExtra = function (){
Return car. protect + "no tariff paid [parent class]";
}
Var BMW = function (){
This. extendf (BMW, car );
This. name = "BMW [subclass ]";
This. start = function (){
Alert (this. name + "exclusive Startup Device! ");
};
Return ("this. name1 =" + this. name );
}
Var BMW2 = function (){
This. extendf (BMW2, car2 );
This. name = "BMW Ultimate 2 [subclass ]";
This. start = function (){
Alert (this. name + "exclusive Startup Device 2 future! ");
};
Return ("this. name1 =" + this. name );
}
Var bensi = {
Name: "bensi ",
Price: "1.3 million ",
Start: function (){
Alert (this. name + "gorgeous startup! ");
},
Stop: function (){
Alert (this. name + "stop the brakes! ");
}
}
Bensi. noExtra = function (){
Return "who dares to collect taxes? ";
}
Var autuo = {
Name: "autuo [subclass ]",
Price: "10 thousand ",
Stop: function (){
Alert (this. name + "Alto failed! ");
}
}
Function ChangAn (){
This. extendf (ChangAn, car );
// This. name = "CHANGAN [subclass ]";
This. run = function (){
Alert (this. name + "is a little slow... ");
}
}
Var ftest = function (){
Var tb = new BMW ("BMW", "0.7 million ");
TestRun (tb );
Alert (BMW. noExtra ());
}
Var ftest2 = function (){
Var tb = bensi // ("Benz", "1.2 million ");
Tb. extendf (bensi, car );
TestRun (bensi );
Alert (bensi. noExtra ());
}
Var ftest3 = function (){
Var tb = new ChangAn ("ChangAn [passing parameters]", "50 thousand ");
TestRun (tb );
Alert (ChangAn. noExtra ());
}
Var ftest4 = function (){
Var tb = autuo
Tb. extendf (autuo, car2 );
TestRun (tb );
Alert (autuo. noExtra ());
}
Var ftest5 = function (){
Var tb = autuo
Tb. extendf (autuo, bensi );
Alert (tb. name );
Tb. start ();
Tb. stop ();
Alert (autuo. noExtra ());
}
Var ftest6 = function (){
Var tb = new BMW2 ("BMW 2", "0.65 million ");
Var scar = document. getElementById ("showcar ");
Scar. innerHTML = tb. remark ();
Alert (tb. name );
Tb. start ();
Tb. stop ();
Alert (BMW2.noExtra ());
}
// Test output
Function testRun (tb ){
Var scar = document. getElementById ("showcar ");
If (! Scar) return false;
Scar. innerHTML = tb. remark ();
Tb. base. start ();
Tb. start ();
Tb. base. run ();
Tb. run ();
Tb. base. stop ();
Tb. stop ();
Alert (tb. extra ("10 thousand"); // This error occurs when the parent class is Object, because the parent class does not
}
</Script>
</Head>
<Body>
Js testing:
<Input type = "button" value = "BMW" onclick = "ftest ()">
<Input type = "button" value = "Mercedes-Benz" onclick = "ftest2 ()">
<Input type = "button" value = "Changan" onclick = "ftest3 ()">
<Input type = "button" value = "Alto" onclick = "ftest4 ()">
<Input type = "button" value = "" onclick = "ftest5 ()">
<Input type = "button" value = "BMW 2" onclick = "ftest6 ()">
<Div id = "showcar"> </div>
</Body>
</Html>
Ps: I have not noticed the performance problem and want to improve it.
I want to use only one parameter. Do you know if you can do this?
The nested class has not been tried.