User.prototype.sayHello=function(){
alert("hello");
}
user.sayHello();这样的话弹出hello
但是我换一种方式
var user=new User();User.prototype={
sayHello:function(){
alert("hello");
}
}user.sayHello();ff报user.sayHello is not a functionjavascriptprototypejs原型

解决方案 »

  1.   

    function User(){
    //code here
    }
    var user=new User();
    User.prototype={
    sayHello:function(){
    alert("hello");
    }
    }报错user.sayHello is not a function
    function User(){
    //code here
    }
    var user=new User();User.prototype.sayHello=function(){
    alert("hello");
    }user.sayHello();弹出hello
      

  2.   

    User.prototype={};
    原型的绑定不能晚于对象的创建,也就是说要在new对象前。
    User.prototype.sayHello = function(){}
    给原型添加一个方法后,该构造函数的实例都会有。
      

  3.   


    function User(){
    //code here
    }
    假设此时User的原型(User.prototype)是User_proto。那么当
    var user=new User()
    user的原型是User_proto。
    此时User.prototype.sayHello=function(){
                alert("hello");
    }
    只是在User_proto上添加了一个sayHello方法。user.sayHello可用。
    但是,当User.prototype={
          sayHello:function(){
                alert("hello");
          }
    }把User类的原型换成。可是user的原型并没有换。还是老的User_proto。所以user.sayHello不可用
      

  4.   

    原型的绑定
    User.prototype.sayHello=function(){
     alert("hello");
    }
    这个算是什么呢,不叫原型绑定么
      

  5.   

    那我在new对象前去修改原型的引用
    function User(){
    //code here
    }
    var methods={
    sayHello:function(){
    alert("hello");
    }
    }
    User.prototype=methods;
    methods={
    sayHello:function(){
    alert("hello");
    },
    sayWord:function(){
    alert("word");
    }
    }
    var user=new User();
    user.sayWord();也是user.sayWord is not a function
      

  6.   


    之前的懂了,谢谢,现在又有个问题function User(){
    //code here
    }
    var methods={
    sayHello:function(){
    alert("hello");
    }
    }
    User.prototype=methods;
    methods={
    sayHello:function(){
    alert("hello");
    },
    sayWord:function(){
    alert("word");
    }
    }
    这样不可以
      

  7.   

    function User(){
                //code here
            }
            var methods={
                sayHello:function(){
                    alert("hello");
                }
            }
            
            methods={
                sayHello:function(){
                    alert("hello");
                },
                sayWord:function(){
                    alert("word");
                }
            }
            User.prototype=methods;
            var user=new User();
            user.sayWord();
      

  8.   

    呵呵之前的知道了,也就是不能在new之后重新赋值User的原型,那样的话new出来的对象还是之前的原型,而通过User.prototype.sayHello则是还在之前的原型上赋予的一个属性而已,谢谢
      

  9.   

    我现在想改变prototype的引用去添加方法
    好像不可以啊
    function User(){
    //code here
    }
    var methods={
    sayHello:function(){
    alert("hello");
    }
    }
    User.prototype=methods;
    methods={
    sayHello:function(){
    alert("hello");
    },
    sayWord:function(){
    alert("word");
    }
    }var user=new User();
    user.sayWord();
      

  10.   

    什么意思呢,大侠,我想改变prototype的引用methods去添加一个方法但是添加不进去啊
      

  11.   

    function User(){
                //code here
            }
    User.prototype={
        sayHello:function(){
            alert("hello");
        },
        sayWord:function(){
            alert("word");
        }
    }
    var user=new User();
    user.sayWord();function User(){
                //code here
            }
    User.prototype={
        sayHello:function(){
            alert("hello");
        }
    }
    var user=new User();
    /*给实例添加一个sayWord方法*/
    User.prototype.sayWord = function(){
            alert("word");
    }
    user.sayWord();
      

  12.   


    User.prototype.sayHello=function(){
                alert("hello");
            }
    你写的这种写法就可以满足你的需求嘛。
    另外,觉得楼主的基础知识很不熟。
    给你推荐一篇文章看看。能领悟多少看你自己了
    http://www.cnblogs.com/RicCC/archive/2008/02/15/JavaScript-Object-Model-Execution-Model.html
      

  13.   

    function User(){
                //code here
            }
            var methods={
                sayHello:function(){
                    alert("hello");
                }
            }
            User.prototype=methods;
            methods.sayWord:function(){
                    alert("word");
                }var user=new User();
            user.sayWord();
      

  14.   

    function User(){
                //code here
            }
            var methods={
                sayHello:function(){
                    alert("hello");
                }
            }
            User.prototype=methods;
            methods.sayWord=function(){
                    alert("word");
                }var user=new User();
            user.sayWord();
      

  15.   

    其实困扰我的是var methods={
      sayHello:"hello"
    }
    var test=methods;
    methods={
      sayWord:"word"
    }
    alert(test.sayWord);//undefined因为对象时引用类型,个人认为之后修改的是methods堆得值,引用未改变所以个人认为解释不通啊