JS注入面向对象思想编程(1)
//JS文件//
//构造函数
(function(){
}())
//基类
function BaseCar(){
//基类属性
this.color="";
this.name="";
this.len="";
}
//基类方法
BaseCar.prototype={
//取得车的基本参数
GetCarParater:function(){
alert("基本参数\r\n车名:"+this.name+",颜色:"+this.color+",长度:"+this.len);
},
//取得车的生产厂家
GetCarFactory:function(str){
alert("生产厂家是:"+str);
}
}
//子类
function Bus(name,color,len){
//初始化父类属性
this.color=color;
this.name=name;
this.len=len;
}
//继承父类的属性和方法
Bus.prototype=new BaseCar();
//重写父类方法
Bus.prototype.GetCarFactory=function(str,address){
alert("巴士生产厂家:"+str+"\r\n地址:"+address);
}
//子类添加新方法
Bus.prototype.GetBusLoad=function(count){
alert("巴士当前载人数是:"+count+"人");
}
//静态变量
Bus.sumLimit=50;
//静态方法
Bus.GetBusSaveLimit=function(){
alert("当前巴士限载人数:"+Bus.sumLimit);
}
html文件调用:
<script type="text/javascript">
function GetObjCar(){
//实例化对象
var busObj=new Bus("红色","奇瑞QQ","2m");
busObj.GetCarParater();
busObj.GetCarFactory("宇通客车","上海市浦东开发区");
busObj.GetBusLoad(60);
Bus.GetBusSaveLimit(); //调用静态方法
}
</script>