Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript函数调用不工作_Javascript_Jquery - Fatal编程技术网

javascript函数调用不工作

javascript函数调用不工作,javascript,jquery,Javascript,Jquery,我仍然习惯于javascript的语法。我试图将动作绑定到按键上,但这里有一些脱节,我无法发现。为什么调用开关案例时没有问题,但它调用的函数从未执行 this.init = function() { $(document).keydown(this.handleKeyPress); } this.handleKeyPress = function(event) { switch (event.which) { case 37: alert

我仍然习惯于javascript的语法。我试图将动作绑定到按键上,但这里有一些脱节,我无法发现。为什么调用
开关
案例时没有问题,但它调用的函数从未执行

this.init = function() {
    $(document).keydown(this.handleKeyPress);
}

this.handleKeyPress = function(event) {
    switch (event.which) {
        case 37:
            alert("key press");        //this executes
            this.turn("left");
            break;
        case 38:
            this.turn("up");
            break;
        case 39:
            this.turn("right");
            break;
        case 40:
            this.turn("down");
            break;
    }
}

this.turn = function(direction) {
    alert(direction);                  //this does not
}

我认为这是唯一相关的代码,但如果有帮助,我很乐意发布更多。谢谢您的帮助。

您很可能遇到了关于“这”的含义的问题。当您在匿名函数内部时,它指的是匿名函数,而不是嵌套在其中的外部函数。您可能希望执行以下操作:

var myObj = this ;
myObj.init = function() {
    $(document).keydown(myObj.handleKeyPress);
}

myObj.handleKeyPress = function(event) {
    switch (event.which) {
        case 37:
            alert("key press");        //this executes
            myObj.turn("left");
            break;
        case 38:
            myObj.turn("up");
            break;
        case 39:
            myObj.turn("right");
            break;
        case 40:
            myObj.turn("down");
            break;
    }
}

myObj.turn = function(direction) {
    alert(direction);                  //this does not
}

最有可能的是,你遇到了关于“这”是什么意思的问题。当您在匿名函数内部时,它指的是匿名函数,而不是嵌套在其中的外部函数。您可能希望执行以下操作:

var myObj = this ;
myObj.init = function() {
    $(document).keydown(myObj.handleKeyPress);
}

myObj.handleKeyPress = function(event) {
    switch (event.which) {
        case 37:
            alert("key press");        //this executes
            myObj.turn("left");
            break;
        case 38:
            myObj.turn("up");
            break;
        case 39:
            myObj.turn("right");
            break;
        case 40:
            myObj.turn("down");
            break;
    }
}

myObj.turn = function(direction) {
    alert(direction);                  //this does not
}

可能是因为
引用的是事件而不是被调用方?你的工作范围是什么?这是否包装在块中?jQuery正在覆盖
handleKeyPress
函数的上下文。为了避免这种情况,您可以自己显式绑定上下文:
$(document).keydown(this.handleKeyPress.bind(this))
如果您在switch语句中
console.log(this)
,我敢打赌
this
不在您期望的上下文中。为什么要使用此?这是在另一个物体里面吗?如果不将您的定义更新为
var turn=function(direction){…}
,请在机箱内部使用
turn('left')
。谢谢大家!问题肯定出在匿名函数中。我试图强迫js看起来像Java:/可能是因为
这个
引用的是事件,而不是被调用方?你的工作范围是什么?这是否包装在块中?jQuery正在覆盖
handleKeyPress
函数的上下文。为了避免这种情况,您可以自己显式绑定上下文:
$(document).keydown(this.handleKeyPress.bind(this))
如果您在switch语句中
console.log(this)
,我敢打赌
this
不在您期望的上下文中。为什么要使用此
?这是在另一个物体里面吗?如果不将您的定义更新为
var turn=function(direction){…}
,请在机箱内部使用
turn('left')
。谢谢大家!问题肯定出在匿名函数中。我试图强迫js看起来像Java:/