如何将事件传递给lamda函数

时间:2018-07-19 01:40:24

标签: javascript events

创建事件监听器时,我不知道如何将事件传递给调用的函数。

class Clickable
{
    constructor()
    {
        document.addEventListener('mousedown', () => this.action());
    }

    action(mouseEvent)
    {
        console.log(mouseEvent); //Undefined
    }
}

1 个答案:

答案 0 :(得分:2)

只需将eventmousedown侦听器传递给这样的函数:

class Clickable {
    constructor() {
        document.addEventListener('mousedown', (e) => this.action(e));
    }
    action(mouseEvent) {
        console.log(mouseEvent);
    }
}