gpt4 book ai didi

typescript - 是否可以扩展 TypeScript 中的内置 Event 类?

转载 作者:搜寻专家 更新时间:2023-10-30 21:24:42 25 4
gpt4 key购买 nike

代码:

class MyEvent extends Event {
constructor(name) {
super(name);
}
}

var event = new MyEvent("mousemove");

运行时错误:

Uncaught TypeError: Failed to construct 'Event': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

有解决办法吗?

最佳答案

Uncaught TypeError: Failed to construct 'Event': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

问题出在 Event v8 运行时中的定义。它不适合基于 class 的扩展。同样的问题曾经因错误而存在,即以下曾经失败:

class MyError extends Error {
constructor(message) {
super(message);
}
}

const error = new MyError("some message");

所以现在。您不能在 TypeScript(或 ES6 类)中扩展 Event 类。

关于typescript - 是否可以扩展 TypeScript 中的内置 Event 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37039638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com