简体   繁体   English

jQuery按键后访问属性

[英]Accessing Properties after jQuery keydown

I have a object called player 我有一个叫玩家的物体

function Player(_x, _y, _speed) {
    this.x = _x;

    this.getX = function() {
        return this.x;
    };

    this.handleKeyDown = function(event) {
        console.log(this.x); // undefined
    };

    $(document.body).keydown(this.handleKeyDown);
}

Why doesn't "handleKeyDown" not see my x Property? 为什么“ handleKeyDown”看不到我的x属性? And how do I work around it? 我如何解决呢?

Because the this in your keydown handler is the element itself (in this case, document.body ). 因为this在你的keydown的处理程序是元素本身(在这种情况下, document.body )。 You need to either 你需要

var self = this;
$(document.body).keydown(function() { self.handleKeyDown() });

Or look at using bind or $.proxy 或者看看使用bind$.proxy

Because this doesn't work the way you think it does in javascript. 因为this无法像您想象的那样在javascript中起作用。 this is the context in which the current function is being executed. this是当前函数正在执行的上下文。 So when you do something like... object.doThing() , then while executing doThing() this is pointing to object. 因此,当您执行诸如... object.doThing() ,则在执行doThing()它指向对象。 However you could call doThing() without the object reference, then this is the global object. 但是,您可以在没有对象引用的情况下调用doThing(), this就是全局对象。

Below is the code you gave rewritten to use the concept of closures to avoid the this schenanigans. 下面是您为使用闭包的概念而编写的代码,以避免出现this假名。 This code can be further refactored to use prototypes and avoid the extra memory overhead of creating all the same functions over and over again but I don't want to junk this up with more information than is needed. 可以对该代码进行进一步的重构,以使用原型,并避免一遍又一遍地创建所有相同功能的额外内存开销,但是我不想将多余的信息浪费掉。

function Player(_x, _y, _speed) {
    var x = _x;

    var getX = function() {
        return x;
    };

    var handleKeyDown = function(event) {
        console.log(x);
    };

    $(document.body).keydown(handleKeyDown);
}

An eye-opening method in Javascript that shows that this does not operate the way you initially thought would be the MDN docs for Function.prototype.apply . Javascript中的一种令人大开眼界的方法,它表明this方法不能像您最初认为的那样是Function.prototype.applyMDN文档

If using ES5, you can use .bind (or on older browsers, shim it, or use $.proxy ) to ensure that this is correctly set: 如果使用ES5,则可以使用.bind (或在较旧的浏览器上进行填充,或使用$.proxy )以确保正确设置了this设置:

this.handleKeyDown = function(event) {
    console.log(this.x);
}.bind(this);

or you can maintain an external reference to this in your lexical scope: 或者您可以在词汇范围内维护this的外部引用:

var self = this;
this.handleKeyDown = function(event) {
    console.log(self.x);
};

this是这里的“ document.body”,而不是对象Player

Because this represents the getX function, try this : 由于this代表了getX函数,请尝试以下操作:

function Player(_x, _y, _speed) {
    var self = this;
    self.x = _x;

    self.getX = function() {
        return self.x;
    };

    self.handleKeyDown = function(event) {
        console.log(self.x);
    };

    $(document.body).keydown(self.handleKeyDown);
}

Edit : meh, I read to fast, I meant handleKeyDown. 编辑:嗯,我读得很快,我的意思是handleKeyDown。 Anyway, the thing is that the context of this changes, so it is a good practice to store it in a self field to clarify your code and make sure you are refering to what you think. 无论如何,事情是的情况下this变化,所以它是将其存储在一个很好的做法self外地澄清你的代码,并确保你是指的你的想法。

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

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