【发布时间】:2015-11-24 17:01:10
【问题描述】:
我只是对Typeahead.js 进行一些研究,它是一个非常酷的库。由于文档也非常好,我已经设法获得了一个基本示例。
但是我试图弄清楚下面的代码块实际上在做什么?
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
在示例中,它是在将预输入初始化为source 选项时传入的。我可以理解它从文本框中获取输入并将其与数据集进行比较,但我对 q 和 cb 是什么感到有点困惑?
【问题讨论】: