ReferenceError: Can't find variable: onPlayerReady (When using callback)

余生长醉 提交于 2019-12-24 21:44:25

问题


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.

Error:

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

CODE:

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');
}

回答1:


'onReady': this.onPlayerReady.bind(this)
'onStateChange': this.onPlayerStateChange.bind(this)


来源:https://stackoverflow.com/questions/58607184/referenceerror-cant-find-variable-onplayerready-when-using-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!