title: Invoke a javascript object method from within a callback
body: <p>I define the following <code>MyClass</code> and its methods in a user script:</p>
  
  <pre><code>function MyClass() {
      this.myCallback = function() {
          alert("MyClass.myCallback()");
      };
  
      this.startRequest = function() {
          GM_xmlhttpRequest({
              'method': 'GET',
              'url': "http://www.google.com/",
              'onload': function (xhr) {
                  myClassInstance.myCallback();
              }
          });
      };
  }
  
  var myClassInstance = new MyClass();
  myClassInstance.startRequest();
  </code></pre>
  
  <p>This script works and the <code>myCallback()</code> method gets called once the <em>GM_xmlhttpRequest</em> completes.</p>
  
  <p>However, it only works because the <code>onload</code> callback is referring to the global variable <code>myClassInstance</code>. If I update the <code>onload</code> callback to:</p>
  
  <pre><code>'onload': function (xhr) {
      this.myCallback();
  }
  </code></pre>
  
  <p>Then I get the (Chrome) error: </p>
  
  <blockquote>
    <p>Uncaught TypeError: Object [object DOMWindow] has no method 'myCallback'.</p>
  </blockquote>
  
  <p>It seems <code>this</code> is being evaluated in the wrong context.</p>
  
  <p>Is there a way to invoke the <code>myCallback()</code> method of <code>myClassInstance</code> without having to resort to using a global variable?</p>