title: Javascript callbacks and "this"
body: <p>I have the following Javascript code, and I'm trying to get a callback to work as shown below.  I want to see an alert with "123" in it.</p>
  
  <pre><code>var A = function(arg){
      this.storedArg = arg;
      this.callback = function(){ alert(this.storedArg); }
  }
  
  var B = function() {
      this.doCallback = function(callback){ callback(); }
  }       
  
  var pubCallback = function(){ alert('Public callback') };
  
  var a = new A(123);
  var b = new B();
  
  b.doCallback(pubCallback); // works as expected
  b.doCallback(a.callback);  // want 123, get undefined
  </code></pre>
  
  <p>I understand what is happening but I'm not sure how to fix it.  How can I get a callback function that references my a object?  In my case, I can make changes to A but not B.</p>