Keywords: constructor inheritance, prototype
JS. js file: function BaseClass (){
This. name = "BaseClass ";
// This. prototype. Height = "1.5"; incorrect syntax
BaseClass. prototype. Height = "1.5 meters ";
This. Width = "2 meters ";
This. Weight = "1 ton ";
This. say = function (){
Return "my name is" + this. name + ", Height:" + this. height + ", Width:" + this. width + ", Weight:" + this. weight;
}
}
Function SonClass (){
This. base = BaseClass;
This. base ();
This. Weight = "2 tons ";
This. Color = "red ";
This. Title = "SonClass ";
This. loudsay = function (){
Return "my name is" + this. title + ", Height:" + this. height + ", Width:" + this. width + ", Weight:" + this. weight + ", Color:" + this. color;
}
}
SonClass. prototype. speed= "100 ";
SonClass. prototype. run = function (){
Return "my name is" + this. title + ", Height:" + this. height + ", Width:" + this. width + ", Weight:" + this. weight + ", Color:" + this. color + ", Speed:" + this. speed;
}
HTML: <Head>
<Title> </title>
<Script src = jS. js type = "text/javascript"> </script>
<Script language = javascript>
// BaseClass baseclass = new BaseClass (); incorrect syntax
Var baseclass = new BaseClass ();
Alert (baseclass. name); // Base
Alert (baseclass. say (); // my name is BaseClass, Height: 1.5 meters, Width: 2 meters, Weight: 1 ton
Var Son = new SonClass ();
Alert (Son. say (); // my name is BaseClass, Height: undefined, Width: 2 meters, Weight: 2 tons
Alert (Son. loudsay (); // my name is SonClass, Height: undefined, Width: 2 meters, Weight: 2 tons, Color: red
Alert (Son. run (); // my name is SonClass, Height: undefined, Width: 2 meters, Weight: 2 tons, Color: red, Speed: 100
SonClass. prototype. Height = "2.5 meters ";
Alert (Son. run (); // my name is SonClass, Height: 2.5 meters, Width: 2 meters, Weight: 2 tons, Color: red, Speed: 100
</Script>
</Head>
<Body>
</Body>
</Html>