Javascript中定义类

动态原型方法:在构造函数内定义非函数属性,而函数属性则利用原型属性定义。

function  Car(sColor,iDoors,iMpg)
    
{
        
this.color=sColor;
        
this.doors=iDoors;
        
this.mpg=iMpg;
        
this.drivers=new Array("Mike","Sue");
        
        
if(typeof Car._initialized=="undefined")
        
{
            Car.prototype.showcolor
=function()
            
{
                alert(
this.color);
            }
;
            Car._initialized
=true;
        }

    }



 
function  test()
    
{
       
var oCar1=new Car("red",4,23);
       alert(oCar1.drivers);
       oCar1.showcolor();
    }

你可能感兴趣的:(Javascript中定义类)