Javascript js:如何在类中使用外部脚本中的函数?
我有一个脚本,我在页面上附加了一个外部JS脚本,Amazon IVS视频播放器。然后,我从Javascript js:如何在类中使用外部脚本中的函数?,javascript,ecmascript-6,es6-class,Javascript,Ecmascript 6,Es6 Class,我有一个脚本,我在页面上附加了一个外部JS脚本,Amazon IVS视频播放器。然后,我从CustomPlayer类创建一个新的CustomPlayer实例 main.js import CustomPlayer from './CustomPlayer.js" var s = document.createElement("script"); s.type = "text/javascript"; s.src = "https:/
CustomPlayer
类创建一个新的CustomPlayer
实例
main.js
import CustomPlayer from './CustomPlayer.js"
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://player.live-video.net/1.3.1/amazon-ivs-videojs-tech.min.js";
$("body").append(s);
const customConfig = { autoPlay: true, controls: true };
const player = new CustomPlayer(customConfig)
import videojs from 'videojs';
class CustomPlayer {
constructor() {
this.player = {};
this.customConfig = customConfig;
}
init() {
this.player = videojs(this.customConfig);
registerIVSTech(this.player);
return this.player;
}
setBitrate() {
// to be added
}
}
export default CustomPlayer;
下面是我的CustomPlayer
课程。它使用了一个名为registerIVSTech()
的方法,自从我在我的main.js
中添加了Amazon IVS player脚本以来,该方法就一直可用,并且是该脚本的一部分
CustomPlayer.js
import CustomPlayer from './CustomPlayer.js"
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://player.live-video.net/1.3.1/amazon-ivs-videojs-tech.min.js";
$("body").append(s);
const customConfig = { autoPlay: true, controls: true };
const player = new CustomPlayer(customConfig)
import videojs from 'videojs';
class CustomPlayer {
constructor() {
this.player = {};
this.customConfig = customConfig;
}
init() {
this.player = videojs(this.customConfig);
registerIVSTech(this.player);
return this.player;
}
setBitrate() {
// to be added
}
}
export default CustomPlayer;
我唯一的限制是我不能使用npm模块,但我觉得我现在使用它的方式假设registerIVSTech
将在那里。有没有更好的方法来处理这个问题?我应该像这样将该方法从窗口传递到类中吗
const player = new CustomPlayer(customConfig, window.registerIVSTech)