class Dog { constructor() { this.name = 'adong';
}
start() { this.p().then(this.say);
}
p() { return new Promise((resolve, reject)=>{
resolve('good');
})
}
say(str) { console.log(this); console.log(this.name + str);
}
}let dog = new Dog();
dog.start();say方法单独调用时没有问题的,但是在Promise的then里面再调用this就变为undefined了,哪个大神帮忙分析一下,谢谢!
undefined (node:5784) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined at say (D:\NodeJS\Test\test2.js:18:22)
BIG阳
相关分类