在点击事件函数中使用 this(类对象)

use*_*024 0 javascript jquery this dom-events

我在按钮上有一个单击事件,我必须传递此类对象才能调用单击事件内的函数。我的要求如下

$(this).on('click', function(){
     this.callFunc();
});
Run Code Online (Sandbox Code Playgroud)

现在,我将其存储在 self 中并在单击函数中使用它。目前我有以下代码来实现此目的。

var self = this;
$(this).on('click', function(){
     self.callFunc();
});
Run Code Online (Sandbox Code Playgroud)

有没有办法直接使用这个里面的点击功能?

Aru*_*hny 5

使用$.proxy将自定义执行上下文传递给回调函数

$(this).on('click', $.proxy(function(){
    this.callFunc();
}, this));
Run Code Online (Sandbox Code Playgroud)