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阳