Javascript 引用错误:Can';找不到变量:onPlayerReady(使用回调时)
我试图使用Javascript 引用错误:Can';找不到变量:onPlayerReady(使用回调时),javascript,jquery,youtube-iframe-api,Javascript,Jquery,Youtube Iframe Api,我试图使用onPlayerReady()和onPlayerStateChange()作为我的YT-Iframe Api包装的回调,但我似乎无法引用我的包装的实例。如何使用回调调用包装器的实例?当我的函数在包装器之外时,我已经让代码正常工作,但我希望它们在包装器内 错误: 代码: onPlayerReady是一个类方法,不是一个独立的变量/函数。您必须通过某个实例或原型来访问它,例如,someInstance.onPlayerReady或Player.prototype.onPlayerReady
onPlayerReady()
和onPlayerStateChange()
作为我的YT-Iframe Api包装的回调,但我似乎无法引用我的包装的实例。如何使用回调调用包装器的实例?当我的函数在包装器之外时,我已经让代码正常工作,但我希望它们在包装器内
错误:
代码:
onPlayerReady是一个类方法,不是一个独立的变量/函数。您必须通过某个实例或原型来访问它,例如,
someInstance.onPlayerReady
或Player.prototype.onPlayerReady
当我尝试这个时。onPlayerReady或Player.prototype.onPlayerReady,我无法访问这个
(onPlayerReady方法中的当前实例)。但它确实被调用。无法在它们驻留的对象的构造函数中引用您想要作为默认参数的方法。请尝试在构造函数体中应用默认选项,您可以在该构造函数体中访问此。。
ReferenceError: Can't find variable: onPlayerReady
Player — player.self-7e1fbb555e29e354bc82162b755809f0b6435967f75b25c572d1953b57364c7c.js:12
onYouTubeIframeAPIReady — player.self-7e1fbb555e29e354bc82162b755809f0b6435967f75b25c572d1953b57364c7c.js:60
(anonymous function) — www-widgetapi.js:141:160
Global Code — www-widgetapi.js:141:215
jquery3.self-e200ee796ef24add7054280d843a80de75392557bb4248241e870fac0914c0c1.js:3842
Helpers = window.Helpers || {};
Helpers.Google = Helpers.Google || {};
Helpers.Google.YT = Helpers.Google.YT || {};
Helpers.Google.YT = {
Player: class Player {
constructor(element = 'ytplayer', options = { playerVars: { controls: 1 },
height: '390',
width: '640',
videoId: 'novideoid',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}}, StubPlayerInstance = null ) {
if (StubPlayerInstance == undefined) {
this.player = new YT.Player(element, options);
} else {
this.player = StubPlayerInstance;
}
}
loadVideoById($element) {
var videoId = $element.data('video-id');
var x = new String(videoId);
this.player.loadVideoById(x);
}
init(modalId){
const thatInstance = this;
$(modalId).on('show.bs.modal', function(e) {
thatInstance.loadVideoById($(e.relatedTarget));
});
return this.player;
}
onPlayerReady(event) {
$('.open-popup').click(function() {
event.target.playVideo();
});
$('.close-popup').click(function(e) {
player.stopVideo();
});
}
onPlayerStateChange(event) {
if(event.data === 0) {
$('.close.close-popup').click();
}
}
}
}
var player;
function onYouTubeIframeAPIReady() {
player = new Helpers.Google.YT.Player().init('#video-modal');
}
'onReady': this.onPlayerReady.bind(this)
'onStateChange': this.onPlayerStateChange.bind(this)