研究javascript的几个例子(一)
关键字:构造继承,prototype
JS.js文件:
HTML:
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;
}
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>
<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>
作者:青羽