<script type="text/javascript"> 
    var ioldFish = function(name,age){ 
      return ioldFish.func.init(name,age);    
    }; 
    ioldFish.func = ioldFish.prototype ={ 
        init:function(name,age){ 
            this.name = name; 
            this.age = age; 
            return this; 
        }, 
        showInfo:function(){ 
            var info = "my name is" + this.name +"i am " +this.age+"years old"; 
            alert(info); 
        } 
    }; 
    ioldFish.func.init.prototype = ioldFish.func; 
    ioldFish(" 老 鱼",27).showInfo(); 
    //var oldFish = new ioldFish("老鱼",27); 
    //alert(oldFish.name); 
</script>
谁能解释解释这句话的含意ioldFish.func.init.prototype = ioldFish.func; 还有这句ioldFish.func = ioldFish.prototype

解决方案 »

  1.   

    这不是模仿jquery的写法?!ioldFish.func = ioldFish.prototype
    这个就是定义一个对象的属性而已
    eg:
     Object obj = new Object();
     obj.prototype = obj.func = ...... //ioldFish.func.init.prototype = ioldFish.func; 
    看下面例子:
       function test(){   }
       var pro = {
         init : function(){     }
       }
       test.prototype = pro; //或者 test.prototype.init = function(){ ...}
       
       var t = new test();
       t.init();
      

  2.   

    貌似会有问题哦alert(ioldFish.func.name)
    ioldFish(" 老 鱼",27).showInfo();
    alert(ioldFish.func.name)本来是undefined的
    运行之后就赋了值
      

  3.   

        var ioldFish = function(name,age){ 
          return ioldFish.func.init(name,age);    
        }; 
        ioldFish.func = ioldFish.prototype ={ 
            init:function(name,age){ 
                this.name = name; 
                this.age = age; 
                return this; 
            }, 
            showInfo:function(){ 
                var info = "my name is" + this.name +"i am " +this.age+"years old"; 
                alert(info); 
            } 
        }; 
        
        //ioldFish(" 老 鱼",27).showInfo(); 
        
        //ioldFish.func.init.prototype = ioldFish.func; 
        //var oldFish = new ioldFish("老鱼",27); 
        //简单点可以这么理解。
        var newClass=function(){};
        /*因为
        ioldFish.func.init=function(name,age){ 
                this.name = name; 
                this.age = age; 
                return this; 
            }
        所以,用newClass代替ioldFish.func.init*/
        newClass=function(name,age){ 
                this.name = name; 
                this.age = age; 
                return this; 
            }
        //而ioldFish.func.init.prototype = ioldFish.func;相当于
        newClass.prototype={
            init:function(name,age){ 
                this.name = name; 
                this.age = age; 
                return this; 
            }, 
            showInfo:function(){ 
                var info = "my name is" + this.name +"i am " +this.age+"years old"; 
                alert(info); 
            } 
        }
        //所以var oldFish = new ioldFish("老鱼",27); 即为
        var oldFish=new newClass("老鱼",27);
        alert(oldFish.name);
        alert(typeof(oldFish.init)); 
      

  4.   

    ioldFish.func.init.prototype这个ioldFish.func.init不是一个方法吗?  在方法上加原型是什么意思?
      

  5.   

    为什么那么写,我只能说是为了封装调用方便吧 - -在js只有对象
    在JavaScript中,prototype对象是实现面向对象的一个重要机制。每个函数就是一个对象(Function),函数对象都有一个子对象prototype对象,类是以函数的形式来定义的。prototype表示该函数的原型,也表示一个类的成员的集合。在通过new创建一个类的实例对象的时候,prototype对象的成员都成为实例化对象的成员。
           1、该对象被类所引用,只有函数对象才可引用;
           2、在new实例化后,其成员被实例化,实例对象方可调用。
           同时,函数是一个对象,函数对象若直接声明成员,不用被实例化即可调用。 
      

  6.   

    用 jquery 为 table里面的行列加事件。都是这样面向对象的做法感觉在写java swing
      

  7.   

    ioldFish.func.init.prototype = ioldFish.func; 
    个人觉得这里是没有用的ioldFish.func = ioldFish.prototype
    这里也没有用感觉程序本身有问题
      

  8.   

    要是还不理解。先看看这个简单的。var B=function(){}
    var A=B.prototype={
    b:123,
    a:function(){alert(this.b)}
    }
    var a=new B();
    a.a()
    A.a();
      

  9.   

    再首位复杂一点儿。var B=function(){}
    var A={
    b:null
    }
    A.b=B.prototype={
        b:123,
        a:function(){alert(this.b)}
    }
    var a=new B();
    a.a()
    A.b.a();
      

  10.   

    <div style="width:20000px;float:left;margin:0 0 0 10px;"></div>
      

  11.   

    ioldFish这个函数,只是个幌子。无论把它当做一个普通函数,还是构造器使用,它的行为都是一样的,即调用ioldFish.func.init函数。
    ioldFish.func = ioldFish.prototype = {...}这一句,是重写了ioldFish的原型,并给它多提供了一个引用。从后上下文看,这里重写ioldFish的原型没有意义,我猜这只是在的抄jQuery而不理解为什么。这里简单的写成ioldFish.func = {...},对于后续的代码毫无影响。
    init方法内部的this引用,指向调用它的对象,即ioldFish.func,所以它的执行结果,是给ioldFish.func增加属性name和age并赋值。
    ioldFish.func.init.prototype = ioldFish.func;将init方法的原型指向ioldFish.func。这里我猜还是照抄jQuery的,而没有理解。由于init方法在上下文中没有被当做构造器使用过,它的原型是什么没有意义。把这一行注释掉,对后续的代码毫无影响。这段代码有趣的地方是,无论调用ioldFish(" 老 鱼",27),还是new ioldFish("老1鱼",27),都只是在操作ioldFish.func并返回ioldFish.func对象本身,可以简单的测试一下:ioldFish(" 老 鱼",27);//为ioldFish.func的name和age属性赋值
    alert(ioldFish.func.name);//显示“ 老 鱼”
    var oldFish = new ioldFish("aaa",27);//再次为ioldFish.func的name和age属性赋值
    alert(ioldFish.func.name);//显示“aaa”
    delete oldFish.name;//删除ioldFish.func的name属性
    alert(ioldFish.func.name);//显示undefined
      

  12.   

    看不懂!!谁能给个jquery的 这中写法的例子 一个个学习哈看
      

  13.   

    等效的代码  function ioldFish(name,age){ 
           this.init(name,age);    
        }; 
        ioldFish.prototype ={ 
            init:function(name,age){ 
                this.name = name; 
                this.age = age; 
            }, 
            showInfo:function(){ 
                var info = "my name is" + this.name +"i am " +this.age+"years old"; 
                alert(info); 
            } 
        }; 
        new ioldFish(" 老 鱼",27).showInfo(); 
      

  14.   


    你的这个代码很容易懂,但上面的代码,我学得ioldFish.func.init.prototype = ioldFish.func; 
    这句话没用,我所以去掉了,测试的进修,依然正确,怎么搞的??????????????