简体   繁体   English

用Babel扩展MediaSource —如何正确调用super()?

[英]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.

相关问题 如何使用 MediaRecorder 作为 MediaSource - How to use MediaRecorder as MediaSource 在一个无类的、纯粹基于函数的扩展类型系统中,如何模拟超级方法的调用(而不是 super() 调用本身)? - Within a class-free, purely function-based, extending type system how does one emulate the invocation of a super method (not the super() call itself)? 扩展原型,继承和超级 - Extending Prototype, Inheritance, and Super 在从Abstract类扩展它时如何使用SUPER类 - How to use SUPER class while extending it from an Abstract class 如何使用 Babel 和 ES6 正确地对 Promise 进行子类化 - How to properly subclass Promises with Babel and ES6 如何使用Babel和Grunt正确编译项目 - How to compile a project properly with Babel and Grunt 如何配置 Babel 以正确处理多个文件夹? - How to configure Babel to properly work with multiple folders? 如何使用Babel正确加载.eot和.woff文件? - How to properly load .eot and .woff files with Babel? ES6(Babel) - 不能在类定义之外调用扩展类的super.methodName - ES6 (Babel) - cannot call super.methodName of an extended class outside the class definition 如果一个类扩展了 Object,Babel 真的不需要调用 super() 吗? - Is it true that Babel doesn't really have to call super() if a class extends Object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM