[英]Extending MediaSource with Babel — how to properly call super()?
I'd like to extend MediaSource. 我想扩展MediaSource。 I'm using Babel.
我正在使用Babel。
class BradMediaSource extends MediaSource {
constructor() {
super();
}
}
const source = new BradMediaSource();
In Chrome directly, this works fine. 直接在Chrome中运行正常。 In a transpiled build done with Babel, I get the following error:
在使用Babel完成的编译构建中,出现以下错误:
Uncaught TypeError: Failed to construct 'MediaSource': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
Uncaught TypeError:无法构造“ MediaSource”:请使用“ new”运算符,此DOM对象构造函数不能作为函数调用。
This seems similar to this GitHub issue: https://github.com/babel/babel/issues/1966 I have also tried the following package, but it doesn't seem to apply to my specific situation... makes no difference: https://www.npmjs.com/package/babel-plugin-transform-custom-element-classes 这似乎类似于此GitHub问题: https : //github.com/babel/babel/issues/1966我也尝试了以下软件包,但它似乎不适用于我的具体情况……没有区别: https://www.npmjs.com/package/babel-plugin-transform-custom-element-classes
My .babelrc
: 我的
.babelrc
:
{ "presets": [ "es2015" ] }
Is there a way around this problem? 有办法解决这个问题吗?
Generally extending builtin types does not work with compiled classes from Babel, so you'd need to configure Babel to not process classes, and limit your application to only browsers that support classes. 通常,扩展内置类型不适用于Babel的已编译类,因此您需要将Babel配置为不处理类,并将您的应用程序限制为仅支持类的浏览器。
Assuming your target browsers all support ES6 class syntax, the easiest approach would be to use babel-preset-env
configured for those target environments. 假设目标浏览器都支持ES6类语法,最简单的方法是使用为那些目标环境配置的
babel-preset-env
。
You can also try to use transform-builtin-extend
in your Babel config for MediaSource
, though that does tend to vary with exactly which things can be extended. 您还可以尝试在Babel配置中为
MediaSource
使用transform-builtin-extend
,尽管它的确随可扩展内容的不同而有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.