本帖最后由 blood2323 于 2012-12-20 12:38:03 编辑

解决方案 »

  1.   

    不是很太明白你的意思?
        function initTopBar(){}
        initTopBar.prototype={
            init:function(id,name,age){
                this.id = id;
                this.name = name;
                this.age = age;
                //这里怎么调用print来打印内容啊
                return print();//这样吗?
            },
            print:function(id,name,age){
                return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
            }
        }
        var p = new initTopBar();
        window.onload = p.init(11,22,33);
      

  2.   

    差不多,但是return print()肯定不行的呀,就是用print的方法输出init的内容
      

  3.   

    设置成initTopBar的静态方法function initTopBar(){};
    initTopBar.prototype={
            init:function(id,name,age){
               this.id = id;
               this.name = name;
               this.age = age;
           //这里怎么调用print来打印内容啊
            },
            print:function(id,name,age){
               return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
            },
    }
    initTopBar.print = function(id,name,age) {
        return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
    };
    initTopBar.print(11,22,33);
    var p = new initTopBar();
    window.onload = p.init(11,22,33);
      

  4.   

    组件写法错误,修改如下:
    function initTopBar(id,name,age){
        this.init(id,name,age);
    };
    initTopBar.prototype={
            init:function(id,name,age){
               this.id = id;
               this.name = name;
               this.age = age;
           //这里怎么调用print来打印内容啊
            },
            print:function(id,name,age){
               return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
            },
    }window.onload = function(){
        var p = new initTopBar();
        p.print();
    }
      

  5.   

    function initTopBar(id,name,age){
        this.init(id,name,age);
    };
    initTopBar.prototype={
            init:function(id,name,age){
               this.id = id;
               this.name = name;
               this.age = age;
           //这里怎么调用print来打印内容啊
            },
            print:function(){
               return console.log("编号:"+this.id+";姓名:"+this.name+";年龄:"+this.age);
            },
    }window.onload = function(){
        var p = new initTopBar();
        p.print();
    }
      

  6.   


    function initTopBar(){};
    initTopBar.prototype={
            init:function(id,name,age){
               this.id = id;
               this.name = name;
               this.age = age;
       return (function(_id,_name,_age){
    this.print(_id,_name,_age);
       }).apply(this,arguments);
           //这里怎么调用print来打印内容啊
            },
            print:function(id,name,age){
               return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
            },
    }
    var p = new initTopBar();
    window.onload = p.init(11,22,33);
    这样是你要的效果吗?
      

  7.   

    不用设置成静态方法也可以,你返回一个匿名函数就可以了。函数函数里面调用print方法。完整代码在7楼。 return (function(_id,_name,_age){
                    this.print(_id,_name,_age);
               }).apply(this,arguments);
      

  8.   


    return (function(_id,_name,_age){
                    this.print(_id,_name,_age);
               }).apply(this,arguments);这个能不能写成通用的,也许别的地方也会经常用到
      

  9.   

    还有一种情况呀,我不是要return的,只是再内部调用一下,这种情况怎么写?大神
      

  10.   

    内部调用直接调用,this.print(),把参数填上就行了。
      

  11.   


    太感谢了,终于对这个有点了解了,然后我又遇到一个问题了,大神帮我看看怎么解决
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <div id="test"></div>
    <button id="123123"/>
    </body>
    </html>
    <script>
    var Class={
        create:function(){
            return function(){
                this.inital.apply(this,arguments);
            };
        }
    };
    /*定义Person类*/
    var Person=Class.create();
    Person.prototype={
        inital:function(id,name,age){
            this.id = id;
            this.name = name;
            this.age = age;
    //this.getInfo();
        },
        getInformation:function(){
     return console.log("编号:"+this.id+";姓名:"+this.name+";年龄:"+this.age)
        },
    getInfo:function(){
    this.getInformation();
    }
    };
    var p = new Person(11,22,33);
    p.getInfo();
    //就是这里,如果上面这么写就是对的,然后放到事件里面就报错了,说 has no method 'getInformation'这是怎么回事情?!!!
    document.getElementById("123123").addEventListener('mouseover',p.getInfo, false);</script>
      

  12.   


    document.getElementById("123123").addEventListener('mouseover',p.getInfo, false);改成document.getElementById("123123").addEventListener('mouseover',function(){
    p.getInfo();
    }, false);这个就是this的问题了,你那种写法,this指向的是button对象。
    还有,不要叫我大神,我只是个菜鸟,不是什么大神。有问题直接问就行了。知道的我会回答。