小編給大家分享一下Promise中this指向問題的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
promisethis代碼
classDog{ constructor(){ this.name='adong'; } start(){ this.p().then(this.say); } p(){ returnnewPromise((resolve,reject)=>{ resolve('good'); }) } say(str){ console.log(this); console.log(this.name+str); } } letdog=newDog(); dog.start();
promisethis錯(cuò)誤顯示
undefined (node:5784)UnhandledPromiseRejectionWarning:TypeError:Cannotreadproperty'name'ofundefined atsay(D:\NodeJS\Test\test2.js:18:22) classDog{ constructor(){ this.name='adong'; } start(){ this.p().then(this.say.bind(this)); } p(){ returnnewPromise((resolve,reject)=>{ resolve('good'); }) } say(str){ console.log(this); console.log(this.name+str); } } letdog=newDog(); dog.start();
以上是“Promise中this指向問題的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!