创建事件监听器时,我不知道如何将事件传递给调用的函数。
class Clickable
{
constructor()
{
document.addEventListener('mousedown', () => this.action());
}
action(mouseEvent)
{
console.log(mouseEvent); //Undefined
}
}
答案 0 :(得分:2)
只需将event
从mousedown
侦听器传递给这样的函数:
class Clickable {
constructor() {
document.addEventListener('mousedown', (e) => this.action(e));
}
action(mouseEvent) {
console.log(mouseEvent);
}
}