Javascript 使用ES6类扩展RTPeerConnection
我正在使用ES6(es2015?)和WebRTC,我希望有一个类来抽象PeerConnection的一些行为。我在两种不同的解决方案中遇到以下问题: 1。使用带前缀的RTPeerConnectionJavascript 使用ES6类扩展RTPeerConnection,javascript,webrtc,ecmascript-6,Javascript,Webrtc,Ecmascript 6,我正在使用ES6(es2015?)和WebRTC,我希望有一个类来抽象PeerConnection的一些行为。我在两种不同的解决方案中遇到以下问题: 1。使用带前缀的RTPeerConnection class Connection extends webkitRTCPeerConnection { constructor() { super(null); //or some config } connect(endpoint) { //D
class Connection extends webkitRTCPeerConnection {
constructor() {
super(null); //or some config
}
connect(endpoint) {
//Do something with the PeerConnection
}
}
var c = new Connection(); //Error here.
这会导致以下错误:
Uncaught TypeError: Failed to construct 'RTCPeerConnection': Please use the 'new' operator, this DOM object constructor cannot be called as a function.
2。使用webrtc adapter.js
import adapter from 'webrtc-adapter';
class Connection extends adapter.RTCPeerConnection {
constructor() {
super(null);
}
connect(endpoint) {
//Error caused when calling this method.
}
}
var c = new Connection(); //Works fine, can use PC methods etc.
c.connect(....); //Uncaught TypeError: c.connect is not a function
我使用带有“es2015”预设的babel传输我的程序,并在Chrome 46中运行,以防有任何不同
我做错什么了吗?向子类添加新方法是否合法?还是有什么与我使用PeerConnection有关?
欢迎提供任何帮助或替代解决方案。至少本机
rtpeerconnection
似乎不可扩展。此外,您可能正在将其传输到ES5,在ES5中子类化根本不起作用。我建议使用正式的、主动管理的方法。它为您提供了符合现代标准的RTPeerConnection和基于es6 promise的API(在Chrome中填充,在Firefox中原生)。谁知道呢,这可能会否定您试图将其子类化的一些原因,我不知道这是否可能。