JS Inheritance Implementation Code _JAVASCRIPT skills

Source: Internet
Author: User
Base.js--The realization of inheritance ==========================
Note: After inheritance, if the parent class is a class, its properties are inherited, methods (including those declared with prototype), static methods, or only properties and methods.
Copy Code code 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)//child class overrides
A[C]=B[C];
}
Return a;//Returns an inherited object
}
if (FA&&FB) {
B.apply (this,a.arguments);
Cha (a,b);
This["Base"] =new b;//Access parent class via 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 Code code as follows:

<script type= "Text/javascript" src= "Base.js" ></script>
<script type= "Text/javascript" >
var car2 = {
Name: "Car" parent "",
Price: "Tens of thousands of" "Parent class",
Start:function () {
Alert (this.name+) has started 2! "Parent class");
},
Run:function () {
Alert (this.name+ "in the driving 2 ... "Parent class");
},
Stop:function () {
Alert (this.name+) has stopped 2! "Parent class");
},
Remark:function () {return "parent class" 2 I am a "+this.name+"; Value "+this.price"
This.remark = "I am a" +this.name+ "; value" +this.price;
}
Car2.prototype.extra = function (EXT) {
Return this.name+ "Tariff 2 is:" +ext;
//}
Car2.protect = "" Parent class "2 protected";
Car2.noextra = function () {
Return car.protect+ "does not pay the Tariff 2" the parent class ";
}
var car = function (Name,price) {
this.name=name| | " Sedan [parent category] ";
this.price=price| | " Tens of thousands of [parent class] ";
This.start = function () {
Alert (this.name+) has started! [Parent class] ");
};
This.run = function () {
Alert (this.name+) in the middle of the ride ... [Parent class] ");
};
this.stop= function () {
Alert (this.name+) has stopped! [Parent class] ");
};
This.remark = function () {return "[Parent class] I'm a" +this.name+ "; value" +this.price;
This.remark = "I am a" +this.name+ "; value" +this.price; Note that doing so name and price will not get the reference, so the comment
}
Car.prototype.extra = function (EXT) {
Return this.name+ "Tariff is [parent class]:" +ext;
}
Car.protect = "[Parent] protected";
Car.noextra = function () {
Return car.protect+ "Do not pay customs [parent class]";
}
var BMW = function () {
THIS.EXTENDF (Bmw,car);
THIS.name = "BMW" sub-Class "";
This.start=function () {
Alert (this.name+ "exclusive boot device!) ");
};
Return ("this.name1=" +this.name);
}
var BMW2 = function () {
THIS.EXTENDF (BMW2,CAR2);
THIS.name = "BMW Ultimate No. 2nd" Subclass "";
This.start=function () {
Alert (this.name+ "exclusive start-up device, No. 2nd future!") ");
};
Return ("this.name1=" +this.name);
}
var Bensi = {
Name: "Bensi",
Price: "1.3 million",
Start:function () {
Alert (this.name+ "gorgeous start!") ");
},
Stop:function () {
Alert (this.name+ "Special brake Stop!") ");
}
}
Bensi.noextra=function () {
Return "Who dares to tax?" ";
}
var Autuo = {
Name: "Autuo" Subclass "",
Price: "10,000",
Stop:function () {
Alert (this.name+ "The Austrian extension has failed!") ");
}
}
function Changan () {
THIS.EXTENDF (Changan,car);
THIS.name = "Changan" Subclass "";
This.run=function () {
Alert (this.name+) walking a bit slow ... ");
}
}
var ftest = function () {
var TB = new BMW ("BMW", "700,000");
TestRun (TB);
Alert (Bmw.noextra ());
}
var ftest2 = function () {
var TB = bensi//("Mercedes", "1.2 million");
TB.EXTENDF (Bensi,car);
TestRun (Bensi);
Alert (Bensi.noextra ());
}
var ftest3 = function () {
var TB = new Changan ("Changan [Reference]", "50,000");
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 2nd", "650,000");
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,000"));//This error occurs when the parent class is object because the parent class itself has no
}
</script>
<body>
JS Test:
<input type = "button" value = "BMW" onclick = "ftest ()" >
<input type = "button" value = "Mercedes" onclick = "ftest2 ()" >
<input type = "button" value = "Changan" onclick = "ftest3 ()" >
<input type = "button" value = "ao tuo" onclick = "ftest4 ()" >
<input type = "button" value = "Mercedes-Benz class" OnClick = "ftest5 ()" >
<input type = "button" value = "BMW No. 2nd" onclick = "Ftest6 ()" >
<div id = "Showcar" ></div>
</body>

PS: did not notice the performance problem, to improve everyone
Want to use only one parameter, do not know that there is no way?
Nested classes have not been tried.

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.