title: How to use requestAnimationFrame with a TypeScript object?
body: <p>I have an object that i want to do drawing on a canvas. It will use <code>requestAnimationFrame</code> to start a game loop:</p>
  
  <p><strong>Contoso.ts</strong></p>
  
  <pre><code>class Contoso
  {
     //private ctx: CanvasRenderingContext2D;
  
     Initialize(ctx: CanvasRenderingContext2D) {
        //this.ctx = ctx;
        Render();
     }
  
     Render() {
        //...snip doing any actual drawing for the purpose of this question
        requestAnimationFrame(this.Render);
     }
  }
  </code></pre>
  
  <p><strong>app.ts</strong></p>
  
  <pre><code>var contoso: Contoso;
  
  contoso = new Contoso();
  contoso.Initialize(canvas);
  </code></pre>
  
  <p>The first time someone calls <code>Initialize</code>, the <code>requestAnimationFrame</code> manages to correctly call <code>Render</code>. </p>
  
  <p>The second time <code>requestAnimationFrame</code> calls <code>Render</code>, the <code>this.Render</code> is <code>undefined</code> and it crashes.</p>
  
  <p>It's almost as though the object was destroyed after the initial call to <code>Initialize</code>.</p>
  
  <p>What is going on?</p>