千家信息网

Promise中this指向问题的示例分析

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,小编给大家分享一下Promise中this指向问题的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!promiset
千家信息网最后更新 2025年11月07日Promise中this指向问题的示例分析

小编给大家分享一下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错误显示

  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指向问题的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0