[英]lodash debounce & ES6 class - this. is not a function
我已經將lodash的反跳連接到了效果很好的輸入,但是我似乎無法調用其他方法。 我嘗試將搜索綁定到我的構造函數中。 這是我的代碼:
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
}
}
您需要交換
this.onLoad();
this.onSearch = _.debounce(this.onSearch.bind(this), 400);
這2行。
否則,在onLoad
您將附加this.onSearch
處理函數,該處理函數尚未綁定去抖動功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.