HarmonyOS bind(this)问题的替代方案

debounce(fn: Function, wait: number = 300) {
    if (this.timeout !== -1) {
      clearTimeout(this.timeout);
    }
    this.timeout = setTimeout(fn.bind(this), wait);
    console.log("==timeout==", this.timeout);
  }
  setFinalInput() {
    console.log("setFinalInput this", JSON.stringify(this));
    this.searchFocus = false;
    this.inputText = this.searchInput;
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

.bind(this)后续不再支持,使用什么方式替代以及为何会有this问题,this问题产生的原因是什么?

HarmonyOS
2024-12-27 15:30:31
1372浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

使用bind(this)的地方可以改成lambda函数形式,ArkTS不允许使用标准库函数Function.bind。标准库使用这些函数来显式设置被调用函数的this参数。在ArkTS中,this的语义仅限于传统的OOP风格,函数体中禁止使用this。适配方案可以参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-more-cases-0000001774279594#ZH-CN_TOPIC_0000001811317882__bind%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95

分享
微博
QQ
微信
回复
2024-12-27 16:50:26
相关问题
HarmonyOS JSONObject替代方案
1101浏览 • 2回复 待解决
ImageReader 在HarmonyOS替代方案
2369浏览 • 1回复 待解决
HarmonyOS netty替代方案
1035浏览 • 1回复 待解决
HarmonyOS Panel替代方案
915浏览 • 1回复 待解决
HarmonyOS scheme是否有替代方案
1116浏览 • 1回复 待解决
GlobalThis替代方案有哪些?
1897浏览 • 1回复 待解决
HarmonyOS import相对路径有无替代方案
813浏览 • 1回复 待解决
图像处理库是否有替代解决方案
1295浏览 • 0回复 待解决
在鸿蒙中netty有好替代方案
934浏览 • 0回复 待解决
HarmonyOS 关于Funtionbind
466浏览 • 1回复 待解决
SDK问题,视频播放替代
712浏览 • 1回复 待解决
HarmonyOS bind提示Address not available
625浏览 • 1回复 待解决
HarmonyOS UDP socket bind失败
806浏览 • 1回复 待解决