title: Why is 'this' undefined inside class method when using promises?
body: <p>I have a javascript class, and each method returns a <code>Q</code> promise. I want to know why <code>this</code> is undefined in <code>method2</code> and <code>method3</code>. Is there a more correct way to write this code?</p>
  
  <pre><code>function MyClass(opts){
    this.options = opts;
  
    return this.method1()
      .then(this.method2)
      .then(this.method3);
  }
  
  MyClass.prototype.method1 = function(){
    // ...q stuff...
  
    console.log(this.options); // logs "opts" object
  
    return deferred.promise;
  };
  
  MyClass.prototype.method2 = function(method1resolve){
    // ...q stuff...
  
    console.log(this); // logs undefined
  
    return deferred.promise;
  };
  
  MyClass.prototype.method3 = function(method2resolve){
    // ...q stuff...
  
    console.log(this); // logs undefined
  
    return deferred.promise;
  };
  </code></pre>
  
  <p>I can fix this by using <code>bind</code>:</p>
  
  <pre><code>function MyClass(opts){
    this.options = opts;
  
    return this.method1()
      .then(this.method2.bind(this))
      .then(this.method3.bind(this));
  }
  </code></pre>
  
  <p>But not entirely sure why <code>bind</code> is necessary; is <code>.then()</code> killing <code>this</code> off?</p>