Why is 'this' undefined inside class method when using promises? [duplicate]
问题 This question already has answers here : setTimeout and “this” in JavaScript (5 answers) Closed 3 years ago . I have a javascript class, and each method returns a Q promise. I want to know why this is undefined in method2 and method3 . Is there a more correct way to write this 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\"