js类 混合的构造函数/原型模式

混合的构造函数/原型模式

联合使用构造函数和原型方式,定义类就非常方便。

<script type="text/javascript">
//定义
    function Car(color,doors)
   {
        this.color=color;
        this.doors=doors;
        this.drivers=new Array("Tom","Jerry");
   }

   Car.prototype.showColor=function(){
        alert(this.color);
   }
   
   //调用:
   var car1=new Car('red',4);
   var car2=new Car('blue',4);
   
   car1.showColor();
   car2.showColor();
   
   alert(car1.drivers);
   car1.drivers.push("stephen");
   alert(car1.drivers); //结果:Tom,Jerry,stephen
   alert(car2.drivers); //结果:Tom,Jerry
   alert(car1 instanceof Car);

</script>

posted @ 2011-06-10 18:50  象牛  阅读(911)  评论(0编辑  收藏  举报