为什么需要函数的原型?我见一些JS的代码,大体如下
function a(){}
a.prototype={}究竟prototype有什么用?

解决方案 »

  1.   

    看一下这里你就知道了:http://blog.csdn.net/xxd851116/archive/2009/04/02/4043336.aspx
      

  2.   


    <script type="text/javascript">
    <!--
    function fnHandler1(name, age){
    this.name = name;
    this.age = age;
    } fnHandler1.prototype.Alert = function(){
    alert(this.name + "---" + this.age);
    } var fn1 = new fnHandler1("Tom", "23");
    fn1.Alert(); function fnHandler2(name, age){
    this.name = name;
    this.age = age;
    this.Alert = function(){
    alert(this.name + "---" + this.age);
    }
    } var fn2 = new fnHandler2("Jack", "24");
    fn2.Alert();
    /*
    两个函数比较下就可以看出fn1的好处就是
    共性的Alert方法不需要每次new都要实例化一次
    *///-->
    </script>