title: how to reference a variable from the parent object in javascript
body: <p>I am trying to write a simple sprite object that will add itself to the canvas when it is initialised:</p>
  
  <pre><code>function Sprite(source){
  this.x = 100;
  this.y = 100;
  this.img = new Image();
  this.img.src = source;
  this.img.onload = function(e){
      context.drawImage(this.img, this.x, this.y);
      };
  }//end of object Sprite
  </code></pre>
  
  <p>This does not work as drawImage needs to access the variables outside of the onload handler. How can i access the variables inside my Sprite object from inside the event handler?</p>