JavaScript helps.
<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>
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);
}
}
JavaScript itself evolved from the syntax of the Perl language, which is essentially a scripting language, as the version of the update gradually added to the object-oriented simulation. I think JS's object-oriented simulation is generally good, because we can not blindly follow any idea, not purely for OOP and OOP, we need to seize the benefits of the object in the end is what? For these advantages to OOP, is the most sensible choice, so that JS did a good job.
JS inheritance in a lot of books carefully divided into a number of types and implementation of the way, is basically two: object posing, prototype mode. Each of these two ways has advantages and disadvantages, here I first enumerate, and then from the bottom of the analysis of the difference:
(i) object posing as
JScript Code
function A (name) {
THIS.name = name;
This.sayhello = function () {alert (this.name+ "Say hello!");
}
Function B (name,id) {
This.temp = A;
This.temp (name); Equivalent to new A ();
Delete this.temp; Prevents the properties and methods of superclass a from being overwritten with temp references at a later time
This.id = ID;
This.checkid = function (ID) {alert (this.id==id)};
}