小编Nug*_*iya的帖子

创建扩展CustomEvent的自定义事件类

我正在尝试使用预定义的选项创建自己的类事件

class TimeoutEvent extends CustomEvent {
    constructor() {
        super(...arguments);

        this.bubbles = true;
        this.cancelable = true;

        this.type = 'timeout';
    }
}

new TimeoutEvent();
Run Code Online (Sandbox Code Playgroud)

但我得到了例外:

Uncaught TypeError: Failed to construct 'CustomEvent': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
Run Code Online (Sandbox Code Playgroud)

我用babel 5.8.23编译它(babel-core 5.8.25)

在编译文件exeption trows就行如下:

 CustomEvent.apply(this, arguments);
Run Code Online (Sandbox Code Playgroud)

我知道真正的方法是使用document.createEvent('Event'); 或者新的CustomEvent(),但这是扩展默认DOM Event类的方法吗?

javascript dom javascript-events extend ecmascript-6

5
推荐指数
0
解决办法
973
查看次数