【问题标题】:lodash debounce & ES6 class - this. is not a functionlodash debounce & ES6 类 - 这个。不是函数
【发布时间】:2019-08-21 00:22:00
【问题描述】:

我已将 lodash debounce 连接到一个运行良好的输入,但我似乎无法调用其他方法。我尝试在构造函数中绑定搜索。这是我的代码:

  class SearchBar {
    constructor() {
      this.searchState = {
        term: '',
        loading: false,
      };
      this.$searchBar = $('.ngaire-search-form > input');

      this.onLoad();
      this.onSearch = _.debounce(this.onSearch.bind(this), 400);
    }

    onLoad() {
      this.$searchBar.on('keyup change', this.onSearch);
    }

    onSearch(e) {
      const searchTerm = e.currentTarget.value;

      if (searchTerm.length > 0) {
        console.log(searchTerm); // this works
        this.verifySearchTerm(searchTerm); // i get this is not a function
      }
    }

【问题讨论】:

    标签: javascript ecmascript-6 lodash


    【解决方案1】:

    你需要交换

      this.onLoad();
      this.onSearch = _.debounce(this.onSearch.bind(this), 400);
    

    这两行。

    否则,在 onLoad 内部附加尚未绑定的去抖动函数的 this.onSearch 处理程序。

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 2019-04-08
      • 2021-08-11
      • 1970-01-01
      • 2020-01-07
      • 2014-08-09
      • 2015-05-21
      • 2019-11-09
      • 2016-07-03
      相关资源
      最近更新 更多