title: Typescript "this" instance is undefined in class
body: <p>I found this and online and now trying to put it in TS.</p>
  
  <p>Running the following throws <code>Uncaught TypeError: Cannot set property 'toggle' of null</code></p>
  
  <pre><code>@Injectable()
  export class HomeUtils {
      private canvas: HTMLCanvasElement;
      private context;
      private toggle = true;
  
      constructor() { }
  
      public startNoise(canvas: HTMLCanvasElement) {
          this.canvas = canvas;
          this.context = canvas.getContext('2d');
          this.resize();
          this.loop();
      }
  
      private resize() {
          this.canvas.width = window.innerWidth;
          this.canvas.height = window.innerHeight;
      }
  
      private loop() {
          this.toggle = false;
          if (this.toggle) {
              requestAnimationFrame(this.loop);
              return;
          }
          this.noise();
          requestAnimationFrame(this.loop);
      }
  
      private noise() {
          const w = this.context.canvas.width;
          const h = this.context.canvas.height;
          const idata = this.context.createImageData(w, h);
          const buffer32 = new Uint32Array(idata.data.buffer);
          const len = buffer32.length;
          let i = 0;
  
          for (; i &lt; len;) {
              buffer32[i++] = ((255 * Math.random()) | 0) &lt;&lt; 24;
          }
  
          this.context.putImageData(idata, 0, 0);
      }
  
  }
  </code></pre>
  
  <p>I'm lost.</p>