關鍵字:構造繼承,prototype
JS.js檔案:function BaseClass(){
this.name="BaseClass";
//this.prototype.Height="1.5米"; 錯誤寫法
BaseClass.prototype.Height="1.5米";
this.Width="2米";
this.Weight="1噸";
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噸";
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: <html>
<head>
<title></title>
<script src=jS.js type="text/javascript"></script>
<script language=javascript>
//BaseClass baseclass = new BaseClass();錯誤寫法
var baseclass = new BaseClass();
alert(baseclass.name); //Base
alert(baseclass.say());//my name is BaseClass,Height:1.5米,Width:2米,Weight:1噸
var Son = new SonClass();
alert(Son.say());//my name is BaseClass,Height:undefined,Width:2米,Weight:2噸
alert(Son.loudsay());//my name is SonClass,Height:undefined,Width:2米,Weight:2噸,Color:red
alert(Son.run());//my name is SonClass,Height:undefined,Width:2米,Weight:2噸,Color:red,Speed:100
SonClass.prototype.Height="2.5米";
alert(Son.run());//my name is SonClass,Height:2.5米,Width:2米,Weight:2噸,Color:red,Speed:100
</script>
</head>
<body >
</body>
</html>