2 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
根據(jù)我的理解,您對(duì)匿名/ lambda 函數(shù)如何對(duì)無(wú)限數(shù)量的參數(shù)進(jìn)行操作感到困惑。
讓我們編寫(xiě)我們自己的過(guò)濾器函數(shù)來(lái)看看它是如何工作的。首先,我們需要兩件事,一個(gè)對(duì)象/值數(shù)組,以及將這些值映射到 true(應(yīng)該在結(jié)果數(shù)組中)或 false(不應(yīng)該在結(jié)果數(shù)組中)的某種方法。然后我們需要做的就是選擇評(píng)估的值以嘗試并返回它們。
// Define array
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
// Define filter
function hasAnE(word) {
return word.includes('e');
}
// Examples
hasAnE(words[0]); // false
hasAnE(words[1]); // false
hasAnE(words[2]); // true
// Create an array for our results
filteredWords = [];
// Go through our words array and check for matches
for (var index = 0; index < words.length; index ++) {
word = words[index];
// Map each element to true / false
filterResult = hasAnE(word);
// If it matches the filter, add it to the results
if (filterResult) {
filteredWords.push(word);
}
}
console.log(filteredWords); // [ 'elite', 'exuberant', 'destruction', 'present' ]
好的,所以我們可以自己構(gòu)建這個(gè)過(guò)濾器功能,但是我們?nèi)绾巫龅絯ords.filter(hasAnE)呢?首先,您需要了解的是,在 JavaScript 中,您可以像傳遞變量一樣傳遞函數(shù)本身。這意味著我們可以編寫(xiě)一個(gè)將另一個(gè)函數(shù)作為參數(shù)的函數(shù)(傳遞的函數(shù)稱為“回調(diào)”)。
function filterWords(wordsArray, filter) {
filteredWords = [];
for (var index = 0; index < wordsArray.length; index ++) {
word = wordsArray[index];
// Map each element to true / false
filterResult = filter(word);
// If it matches the filter, add it to the results
if (filterResult) {
filteredWords.push(word);
}
}
return filteredWords;
}
console.log(filterWords(words, hasAnE)); // [ 'elite', 'exuberant', 'destruction', 'present' ]
請(qǐng)注意,我們得到了與之前相同的結(jié)果。JavaScript 不僅允許我們將過(guò)濾器作為輸入?yún)?shù)傳遞,而且還可以無(wú)縫調(diào)用它。但是我們?nèi)绾螌⑦^(guò)濾器直接應(yīng)用到 words 數(shù)組本身呢?這是原型派上用場(chǎng)的地方,因?yàn)樗试S我更改現(xiàn)有類(lèi)的“基本”代碼。例如,Array.prototype讓我可以訪問(wèn) Array 類(lèi)(我們的 words 數(shù)組就是其中之一)的所有默認(rèn)字段和方法。因此,利用我們對(duì)函數(shù)可以存儲(chǔ)為變量的知識(shí),我們可以這樣做:
function filterWordsPrototype(filter) {
filteredWords = [];
for (var index = 0; index < this.length; index ++) {
word = this[index];
// Map each element to true / false
filterResult = filter(word);
// If it matches the filter, add it to the results
if (filterResult) {
filteredWords.push(word);
}
}
return filteredWords;
}
Array.prototype.filterWords = filterWordsPrototype;
console.log(words.filterWords(hasAnE)); // [ 'elite', 'exuberant', 'destruction', 'present' ]
這里沒(méi)有魔法。我們的最終words.filterWords(hasAnE)看起來(lái)非常接近原始words.filter(someFunction)注意我如何擺脫數(shù)組參數(shù)并將對(duì)數(shù)組的所有引用this改為。那是因?yàn)槲覍⒃摵瘮?shù)設(shè)置為數(shù)組本身(以及所有數(shù)組)的方法,所以this現(xiàn)在指的是我正在調(diào)用該函數(shù)的數(shù)組。
現(xiàn)在,實(shí)際的實(shí)現(xiàn)當(dāng)然比這更有效、更安全、更詳細(xì),但我希望我已經(jīng)回答了你關(guān)于幕后大致發(fā)生的事情的問(wèn)題。

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
正如丹尼爾在評(píng)論中指出的那樣,您的函數(shù)是一個(gè)回調(diào)函數(shù)(因?yàn)椴灰{(diào)用 use 我們稍后會(huì)調(diào)用您)。
在引擎蓋下,數(shù)組過(guò)濾器函數(shù)是這樣實(shí)現(xiàn)的:
Array.prototype.filter = function (callback) {
const goodValues = []
for (let i = 0; i < this.length; i++) {
if (callback(this[i])) { // your callback is used here
goodValues.push(this[i])
}
}
return goodValues
}
它的要點(diǎn)是您提供一個(gè)函數(shù)(函數(shù)也是對(duì)象)作為參數(shù)。然后 filter 的過(guò)濾器實(shí)現(xiàn)將使用您的函數(shù)來(lái)確定它是否應(yīng)該保留一個(gè)值或?qū)⑵鋪G棄。
在Array.prototype你每次創(chuàng)建新陣列只是手段[],將有filter功能。盡管強(qiáng)烈討論,您也可以將自己的函數(shù)添加到數(shù)組中。有時(shí),dump如果您鏈接許多地圖和過(guò)濾器,我會(huì)得到一個(gè)幫助來(lái)理解中間值。
添加回答
舉報(bào)