ReferenceError:找不到变量:onPlayerReady(使用回调时) [英] ReferenceError: Can't find variable: onPlayerReady (When using callback)

查看:210
本文介绍了ReferenceError:找不到变量:onPlayerReady(使用回调时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用onPlayerReady()onPlayerStateChange()作为YT Iframe Api包装器的回调,但是我似乎无法引用我的包装器的实例.我该如何使用回调来调用包装器的实例?当函数不在包装器中时,我的代码可以工作,但我希望将其包含在包装器中.

I am trying to use onPlayerReady() and onPlayerStateChange() as call backs for my YT Iframe Api wrapper, but I can't seem to reference an instance of my wrapper. How do I go about using the call back to call an instance of my wrapper? I have gotten my code to work when my functions are outside of my wrapper, but I would love to have them inside the wrapper.

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)

这篇关于ReferenceError:找不到变量:onPlayerReady(使用回调时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆