本帖最后由 h123hu 于 2012-03-16 12:25:42 编辑

解决方案 »

  1.   

    我也不咋明白,function Odemo(name)
    {
    this.name=name;

    if(typeof sayName!='function')
    {
    Odemo.prototype.sayName=function()
    {
    return this.name;
    }
    }
    }
     var a=new Odemo('小王',25);   //这里没有作用
    Odemo.prototype={
    name:'包包',
    sayName:function()
    {
    return this.name+" 跟 小李";
    }
    } var b=new Odemo('小王',25); alert(a.sayName());    //显示小王 alert(b.sayName());    //显示小王 alert(b.name);    //显示小王如果说原型地址改了,为啥后面两个还是显示 小王
      

  2.   

    //这里没有作用
              Odemo.prototype={
                        name:'包包',
                        sayName:function()
                        {
                            return this.name;
                        }
                    }                alert(a.sayName());    //显示小王
    这一段是用点字面量的形式来创建原型对象,创建完成后构造函数的指针不指向新创建的原型对象,所以不会起作用,
    //这里有作用
                    Odemo.prototype.sayName=function(name){
                        return this.name=name;
                    }                alert(a.sayName());    //显示undefined
    这一段是重写了原型对象中的sayName方法,所以会立即生效。
      

  3.   

    第一个是重写了原型,原型里面的name属性还是”包包“,实列里面的属性是“小王”,实列属性要优先查找,所以是小王,如果去掉实例里面的属性,就会显示“包包”,如下:
     function Odemo(name)
                    {
                        //this.name=name;
                        
                        if(typeof sayName!='function')
                        {
                            Odemo.prototype.sayName=function()
                            {
                                return this.name;
                            }
                        }
                    }
                    
                    
                    
                  
              
                   //这里没有作用
              Odemo.prototype={
       constuctor:Odemo,
                        name:'包包',
                        sayName:function()
                        {
                            return this.name;
                        }
                    }
     var a=new Odemo();
                    alert(a.sayName());    //显示包包第二个没有穿参数,所以是未定义
      

  4.   


    要这样写才起作用
    var b=Odemo.prototype;

    alert(a.sayName()); //显示小王

    alert(b.sayName()); //显示包包跟小李

    alert(b.name); //显示包包