简体   繁体   English

原型内部的功能不起作用

[英]Function inside prototype not working

The onKeyDown function is not called when the keydown event is fired using the following code: 使用以下代码触发keydown事件时,不会调用onKeyDown函数:

Game.prototype.setEventHandlers = function() {
    // Keyboard events
    window.addEventListener("keydown", onKeyDown, false);
    window.addEventListener("downup", onKeyUp, false);

    // Mouse events
    window.addEventListener("mousemove", onMouseMove, false);
    window.addEventListener("click", onMouseClick, false);

    // Window events
    window.addEventListener("resize", onWindowResize, false);

    var onKeyDown = function(e) {
        alert("HI!");
    };
}

If I replace it with the traditional function syntax it works just fine: 如果我将其替换为传统的函数语法,则可以正常工作:

function onKeyDown() {
    alert("HI!");
}

Any ideas as to why the var onKeyDown = function syntax doesn't get the job done? 关于为什么var onKeyDown = function语法无法完成工作的任何想法? Thanks in advance. 提前致谢。

Your order of operations is the reason that the window.addEventListener calls are failing. 您的操作顺序是window.addEventListener调用失败的原因。

You're never passing the onKeyDown function in the first version, you're passing the value of the onKeyDown variable , which at the time of calling is undefined . 您永远不会在第一个版本中传递onKeyDown 函数 ,而是传递onKeyDown 变量的值,该变量在调用时为undefined

The reason the "traditional" version works, is due to what's called "hoisting". “传统”版本起作用的原因是由于所谓的“吊装”。

var and function declarations are hoisted to the top of the function that they are written in. varfunction声明被提升到所写函数的顶部。

Even though you might write code as: 即使您可能将代码编写为:

Shortened for demonstration purposes 出于演示目的而缩短
 Game.prototype.setEventHandlers = function() { // Keyboard events window.addEventListener("keydown", onKeyDown, false); var onKeyDown = function(e) { alert("HI!"); }; } 

It actually executes as: 它实际执行为:

 Game.prototype.setEventHandlers = function() { var onKeyDown; // Keyboard events window.addEventListener("keydown", onKeyDown, false); onKeyDown = function(e) { alert("HI!"); }; } 

If you were to use a function declaration instead 如果要改用函数声明

IE IE浏览器
Game.prototype.setEventHandlers = function() {
    function onKeyDown(e) {
        alert("HI!");
    }

    // Keyboard events
    window.addEventListener("keydown", onKeyDown, false);
}

The code actually executes as: 该代码实际执行为:

 Game.prototype.setEventHandlers = function() { function onKeyDown(e) { alert("HI!"); } // Keyboard events window.addEventListener("keydown", onKeyDown, false); } 

Code cleanup tools such as jslint will flag these sorts of inconsistencies. 诸如jslint之类的代码清除工具将标记这些类型的不一致。 It's generally safer to write JS code in the way that it's going to execute. 通常,以将要执行的方式编写JS代码更为安全。 This helps prevent this sort of order of operations mistake. 这有助于防止这种操作顺序错误。

Any ideas as to why the var onKeyDown = function syntax doesn't get the job done? 关于为什么var onKeyDown =函数语法无法完成工作的任何想法?

At the moment you pass onKeyDown to addEventListener , it doesn't have a value yet. 目前,您将onKeyDown传递给addEventListener ,它还没有值。 The assignment of the value takes place at the end of the function, after you already called addEventListener . 调用addEventListener之后,值的分配在函数的末尾进行。

Move the assignment to the top: 将作业移到顶部:

Game.prototype.setEventHandlers = function() {

    var onKeyDown = function(e) {
        alert("HI!");
    };

    // Keyboard events
    window.addEventListener("keydown", onKeyDown, false);
    window.addEventListener("downup", onKeyUp, false);

    // Mouse events
    window.addEventListener("mousemove", onMouseMove, false);
    window.addEventListener("click", onMouseClick, false);

    // Window events
    window.addEventListener("resize", onWindowResize, false);

}

Function declarations ( function name() {} style) have their variable and value hoisted to the top of the scope, but anonymous functions assigned to a variable ( var fn = function() {} style) are not. 函数声明( function name() {}样式)的变量和值都放在作用域的顶部,但是分配给变量的匿名函数(变量var fn = function() {}样式)则没有。 In your code, the variable declaration is hoisted but not the assignment of the function to that variable. 在您的代码中,将悬挂变量声明,而不是将函数分配给该变量。

So your code is actually being parsed like this: 因此,您的代码实际上是这样解析的:

Game.prototype.setEventHandlers = function() {
  var onKeyDown;

  window.addEventListener("keydown", onKeyDown, false);

  onKeyDown = function(e) { ... };
};

So onKeyDown is undefined when you pass it to addEventListener . 因此,当您将onKeyDown传递给addEventListener时,它是未定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM