ReferenceError: Can't find variable: onPlayerReady (When using callback)
ReferenceError: Can't find variable: onPlayerReady (When using callback)
我正在尝试使用 onPlayerReady()
和 onPlayerStateChange()
作为我的 YT Iframe Api 包装器的回调,但我似乎无法引用我的包装器的实例。我如何着手使用回调来调用我的包装器实例?当我的函数在我的包装器之外时,我的代码可以工作,但我希望将它们放在包装器内。
错误:
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)
我正在尝试使用 onPlayerReady()
和 onPlayerStateChange()
作为我的 YT Iframe Api 包装器的回调,但我似乎无法引用我的包装器的实例。我如何着手使用回调来调用我的包装器实例?当我的函数在我的包装器之外时,我的代码可以工作,但我希望将它们放在包装器内。
错误:
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)