title: JavaScript addEventListener add a reference to the current object
body: <p>I have simplified my code to illustrate the problem.</p>
  
  <pre><code>function SnakeGame ()
  {
      this.snakeDirection         = 'right';
  
      this.Init = function ()
      {
          window.addEventListener('keydown', this.keyboardInput, false);
      }
  
      this.keyboardInput = function (event, SnakeGameObject)
      {
          console.log(SnakeGameObject); //Error since I can't pass this variable...
          console.log(event.keyCode); //Works
      }
  }
  </code></pre>
  
  <p>Within the this.keyboardInput function I try to change the variable this.snakeDirection; problem is that I can't get a reference to the SnakeGame object. Within the keyboardInput function this refers to window. I understand why it refers to window, but I can't think of a solution...</p>
  
  <p>The full code can be seen here: <a href="http://eriknijland.nl/stackoverflow/snake_event_listener/" rel="nofollow">http://eriknijland.nl/stackoverflow/snake_event_listener/</a></p>