title: Javascript: Callback in constructor
body: <p>I'm trying to write OO javascript for an object that has an expensive initialization process that will callback a function when its done.</p>
  
  <p>The problem is that the caller needs to use the functions of that same object in the callback routine, and the object doesn't exist yet:</p>
  
  <pre><code>// ctor for foo object
  function foo(callback) {
  
      // do slow initialization here..
  
      // callback when done
      callback();
  };
  
  foo.prototype = function() {
  
      return {
          // doStuff method
          doStuff: function() {
            alert('stuff done');
          }
      };        
  }();
  
  // instantiate the foo object, passing in the callback
  var f = new foo(function() {
  
      //Uncaught TypeError: Cannot call method 'doStuff' of undefined 
      f.doStuff();
  
  });​
  </code></pre>
  
  <p><a href="http://jsfiddle.net/fmEbb/1/">jsFiddle</a>
  What am I missing here?</p>