简体   繁体   English

如何使用“this”获取事件处理程序中的类变量

[英]How do get class variable within an event handler with “this”

Here is some code: 这是一些代码:

var class = function(elem,div){

    this.elem= elem;
    this.div = div;

    this.init = function(){
        this.div.bind('keyup',this.handler);
    }

    this.handler= function(event){
        //HOW DO I GET "this.elem" ???
        //here, this = div
    }

    this.init();
}

I want to get the variable "elem" from within my "handler" function, but everytime i call this.elem, the "this" is referring to the elem that was bound to the event handler!!. 我想从我的“处理程序”函数中获取变量“elem”,但每次调用this.elem时,“this”指的是绑定到事件处理程序的元素!!

Well, you could just reference elem . 好吧,你可以参考elem

Or you could declare var that = this; 或者你可以声明var that = this; outside of the the handler and then reference that.elem . 在处理程序之外,然后引用that.elem

I suspect you are registering this.handler as the event handler itself. 我怀疑你正在将this.handler注册为事件处理程序本身。 In this case, the method is not being executed in the context of the object; 在这种情况下,该方法不在对象的上下文中执行; it's being executed like any other event handler. 它像任何其他事件处理程序一样被执行。

Try writing a simple handler that invokes your handler method on an instance of class. 尝试编写一个简单的处理程序,在类的实例上调用handler方法。

var instance = new class(...); // populate args that make sense 
document.yourElement.onclick = function(){
   instance.handler();
}

Also, you really should use prototype to define instance methods. 此外,您真的应该使用prototype来定义实例方法。 Doing it the way you are doing it is very inefficient. 按照你的方式做它是非常低效的。

var class = function(elem,div){

    this.elem= elem;
    this.div = div;

    this.init();
}


class.prototype.init = function(){
        this.div.bind('keyup',this.handler);
    }

class.prototype.handler= function(event){
        //HOW DO I GET "this.elem" ???
        //here, this = div
    }

If you're cool in using ES5 , you might want to invoke the Function.prototype.bind method. 如果您使用ES5很酷,则可能需要调用Function.prototype.bind方法。 Like 喜欢

this.handler= function(event){
    //HOW DO I GET "this.elem" ???
    //here, this = div
}.bind(this)

There are also lots of shims for that method available to gracefully support old'ish browsers. 这种方法还有很多垫片可以优雅地支持旧的浏览器。 The above code would cause that the function which is stored in this.handler gets bound to the value of this when the method is called like new class(); 上面的代码会导致在this.handler方法时将this.handler存储的函数绑定到this的值,如new class(); forever. 永远。

elem is captured in a closure, simply reference it as elem : elem被捕获在一个闭包中,只需将其引用为elem

this.handler= function (event) {
     //HOW DO I GET "this.elem" ???
     //here,

     elem; // elem from the constructor.

     this = div
} 

Also

  1. there are no classes in javascript. javascript中没有类。

  2. class is a reserved word and you should not use it for an identifier name. class是保留字,不应将其用作标识符名称。

  3. I guess from your names and the way you used them that elem is a DOM element and div is a DIV element. 我想从你的名字和你使用它们的方式来看, elem是一个DOM元素而div是一个DIV元素。 If so, they don't have a bind method. 如果是这样,他们没有绑定方法。 If you want to assign a listener, then: 如果要分配侦听器,则:

     this.onkeyup = this.handler; 

but that must be placed after you define this.handler . 但必须在定义this.handler后放置。

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

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