title: accessing class data on window function typescript
body: <p>I am building an application with Angular 2 and I'm also using the YouTube IFrame API. </p>
  
  <p>The Youtube API requires me to declare some functions to the global object, so this is my code:</p>
  
  <pre><code>export class MyClass {
  
    someData: any;
  
    constructor( ... ) {
      ...
    }
  
    loadAPI(){
      (window as any).onYouTubeIframeAPIReady = function () {
        buildPlayer();
      }
  
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      console.log('API loaded');
    }
  
  }
  </code></pre>
  
  <p>It works fine, then, outside of the class I have this: </p>
  
  <pre><code>function buildPlayer(){
    player = new YT.Player('player', {
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
    console.log('youtube iframe api ready!');
  }
  
  function onPlayerReady(event){
    // HERE IS MY PROBLEM
    // I want to manipulate "someData" in these functions
  }
  
  function onPlayerStateChange(status){
    // HERE IS MY PROBLEM
  }
  </code></pre>
  
  <p>I want to manipulate some data that comes from MyClass (that is retreiving it from the database) inside these functions, but when I try to access it with "this.data" it says it's undefined. How can I do it?</p>