<html>
<head>
<script type="text/javascript">
function game(){//game类
}
game.prototype={
show:function(){
alert(1);
},
onkey:function(){
this.show();//在此处想调用show函数。当前情况下this不是指向game的(所以这种写法是错的),怎么才能调用到show函数
},
init:function(){
document.onkeydown=this.onkey;
}
}
var t=new game();
</script>
</head>
<body onload="t.init()">
</body>
</html>

解决方案 »

  1.   

    <html>
        <head>
            <script type="text/javascript">
                function game(){//game类
                }
                game.prototype={
                    show:function(){
                        alert(1);
                    },
                    onkey:function(){
                        t.show();//在此处想调用show函数。当前情况下this不是指向game的(所以这种写法是错的),怎么才能调用到show函数
                    },
                    init:function(){
                        document.onkeydown=this.onkey;
                    }
                }
                var t=new game();
            </script>
        </head>
        <body onload="t.init()">
        </body>
    </html>
    这样可以,你试试
      

  2.   

    LS 的方法太局限了 万一我是定义 var m=new game();那就不起作用了,
    所以还是希望在类的内部能解决。
    另外 2楼,你不是女的38节就不关你事了?你老婆,或者你老妈,不是女的吗。
      

  3.   

    我是菜鸟
    你的问题太标题当了
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
             <script type="text/javascript">
                function game(){//game类
                }
                game.prototype={
                    show:function(){
                        alert(1);
                    },
                    onkey:function(){
                        this.show();//在此处想调用show函数。当前情况下this不是指向game的(所以这种写法是错的),怎么才能调用到show函数
                    },
                    init:function(){
    var self = this;
                        document.onkeydown = function(){self.show()};
                    }
                }
    window.onload = function(){
    new game().init();
    }
            </script>
    <body >
    </body>
    </html>
      

  4.   


    <html>
        <head>
         <script type="text/javascript">
                function game(){//game类
                }
                game.prototype={
                    show:function(){
                        alert(1);
                    },
                    onkey:function(){
                        this.show();
                    },
                    init:function(){
    var _this = this;
                        document.onkeydown = function() {_this.onkey()};
                    }
                }
                var t=new game();
            </script>
        </head>    <body onload="t.init()">    </body>
    </html>
      

  5.   

    LS的方法是可以调用到show函数。但是我希望是在onkey:function(){}
    这个函数中能够调用到show(),我有特殊需求,onkey中代码比较长,这里只是
    简化了下。
      

  6.   

    lz可耻
    我的写法和6l一样
    而且还节省了一个全局变量标题党+可耻.............