function car(sColor){
this.color=sColor;
if(typeof car._initialized =="undefined"){
car.prototype.show=function(){
alert(this.color);
}
}
}car.prototype.show=function (){
alert("hello world");
}var s=new car();
s.show();
为什么不输出hello world,而输出undefined

解决方案 »

  1.   

    你每次new car()的时候,car()里的代码都会运行一次
    而在car()里因为typeof car._initialized =="undefined"成立,所以每当new car()的时候,car.prototype.show都会被修改成为 
    function(){ 
     alert(this.color); 

    而你的new car()中没有参数,所以得到undefined
      

  2.   

    我后面的car.prototype.show=function (){ 
    alert("hello world"); 
    }
    不可以把前面的方法覆盖掉么,
      

  3.   

    是覆盖了没错,但是倒数第二句var s=new car(); 运行的时候,又覆盖回去了