javascript - ES6 类 : access to 'this' with 'addEventListener' applied on method

标签 javascript dom ecmascript-6 dom-events

<分区>

在这个 es6 脚本中,点击事件不起作用,因为 sayHello使用 this.elm 调用方法( <div> ) 作为 this .

如何在不松开范围的情况下将事件关联到方法?

class player{
  constructor (name) {
    this.name = name;
    this.elm = document.createElement('div');
    this.elm.addEventListener('click', this.sayHello);
  }
  sayHello() {
    console.log(this.name + ' say: "hello!"'); // 'undefined say 'hello!"';
  }
  kill() {
    console.log(`RIP ${this.name} :'(`); 
    this.elm.addClass('dead');
    this.elm.removeEventListener('click', this.sayHello);
  }
}

最佳答案

这是一个通用的JS问题,但它的核心在于

this.elm.addEventListener('click', this.sayHello);

没什么不同
var fn = this.sayHello;
this.elm.addEventListener('click', fn);

您正在传递一个函数作为事件处理程序,但未确保在调用 fnthis 将设置为您想要的值。在 ES5 中执行此操作的最简单方法是

this.elm.addEventListener('click', this.sayHello.bind(this));

或者在 ES6 中,使用箭头函数:

this.elm.addEventListener('click', evt => this.sayHello(evt));

但是请注意,这两种解决方案都会破坏您在 kill 中的(已经略微破坏的)逻辑,因为

this.elm.removeEventListener('click', /* what? */);

您不再对附加的函数有任何引用,因此您无法删除事件处理程序。

我建议两种选择:

// Create a new function that is bound, and give it a new name
// so that the 'this.sayHello()' call still works.
this.boundSayHello = evt => this.sayHello(evt);
this.elm.addEventListener('click', this.boundSayHello);
this.elm.removeEventListener('click', this.boundSayHello);

// Bind the function with the same name and use `.bind` instead of the
// arrow function option.
this.sayHello = this.sayHello.bind(this);
this.elm.addEventListener('click', this.sayHello);
this.elm.removeEventListener('click', this.sayHello);

关于javascript - ES6 类 : access to 'this' with 'addEventListener' applied on method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30446622/

相关文章:

用于在两个数组中查找公共(public)元素的 Javascript 程序

javascript - 与网页上的图表交互

javascript - 为什么onclick的处理函数没有按预期工作?

xpath 后的 PHP DomXPath 编码问题

javascript - handleSubmit 和 onChange handlerSubmit 导致 setState 错误或无法输入字符

javascript - 使用 ES6 文件进行 Stryker 突变测试

javascript - 从 firebase 接收数据

javascript - 找出 dom 中的元素在 Javascript 中出现在哪个行号上?

javascript - 内存游戏 javascript 重置功能仅适用于最后两次点击

javascript - KonvaJS,可以用掩码代替clipFunc吗?