使用匿名函数作为javascript对象的一部分

Using an anonymous function as part of a javascript object

本文关键字:javascript 对象 一部分 函数      更新时间:2023-09-26

我将向您展示两个片段。

this.searchBox = new Foo.UI.SearchBox(this.input, {
    autoCompleteSearchComplete: processSearchResults
});

这根本不起作用:

this.searchBox = new Foo.UI.SearchBox(this.input, {
    autoCompleteSearchComplete: function() {
        processSearchResults
    }
});

我需要将processSearchResults调用放在if语句中,以检查我的搜索文本输入($('.search'))是否有任何文本写入其中。

我的第一个想法是使用这个函数类型符号,但它不起作用。这就好像从来没有调用过processSearchResults

有什么建议吗?

这是因为您实际上并没有调用该函数。这是正确的:

this.searchBox = new Foo.UI.SearchBox(this.input, {
    autoCompleteSearchComplete: function() {
        if (...) {
            processSearchResults();
        }
    }
});