第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我們?nèi)绾卧趈avascript中的函數(shù)內(nèi)部理解和創(chuàng)建這個參數(shù)?

我們?nèi)绾卧趈avascript中的函數(shù)內(nèi)部理解和創(chuàng)建這個參數(shù)?

ibeautiful 2021-08-20 10:12:57
我是 JS 語言的新手。我對用函數(shù)創(chuàng)建的變量聲明有很多困惑。創(chuàng)建和調(diào)用時通常如下:function sum(a, b) {    return a + b}我們知道 a, b 可以稍后被值調(diào)用,毫無疑問console.log(sum(1, 2))但是我對內(nèi)置函數(shù)和一般情況下傳遞的參數(shù)有疑問。他們是:   var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];const result = words.filter(function (word) { return word.length > 6} );console.log(result);匿名函數(shù)中的這個詞參數(shù)是如何抓取數(shù)組中的每個元素的?這背后的概念是什么?不僅在這個內(nèi)置函數(shù)中,這也是我對 Javascript 的一般疑問。每次都讓我困惑。請有人向我解釋如何有效地創(chuàng)建和使用這種參數(shù)。
查看完整描述

2 回答

?
滄海一幻覺

TA貢獻1824條經(jīng)驗 獲得超5個贊

根據(jù)我的理解,您對匿名/ lambda 函數(shù)如何對無限數(shù)量的參數(shù)進行操作感到困惑。


讓我們編寫我們自己的過濾器函數(shù)來看看它是如何工作的。首先,我們需要兩件事,一個對象/值數(shù)組,以及將這些值映射到 true(應(yīng)該在結(jié)果數(shù)組中)或 false(不應(yīng)該在結(jié)果數(shù)組中)的某種方法。然后我們需要做的就是選擇評估的值以嘗試并返回它們。


// 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)建這個過濾器功能,但是我們?nèi)绾巫龅絯ords.filter(hasAnE)呢?首先,您需要了解的是,在 JavaScript 中,您可以像傳遞變量一樣傳遞函數(shù)本身。這意味著我們可以編寫一個將另一個函數(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' ]

請注意,我們得到了與之前相同的結(jié)果。JavaScript 不僅允許我們將過濾器作為輸入?yún)?shù)傳遞,而且還可以無縫調(diào)用它。但是我們?nèi)绾螌⑦^濾器直接應(yīng)用到 words 數(shù)組本身呢?這是原型派上用場的地方,因為它允許我更改現(xiàn)有類的“基本”代碼。例如,Array.prototype讓我可以訪問 Array 類(我們的 words 數(shù)組就是其中之一)的所有默認字段和方法。因此,利用我們對函數(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' ]

這里沒有魔法。我們的最終words.filterWords(hasAnE)看起來非常接近原始words.filter(someFunction)注意我如何擺脫數(shù)組參數(shù)并將對數(shù)組的所有引用this改為。那是因為我將該函數(shù)設(shè)置為數(shù)組本身(以及所有數(shù)組)的方法,所以this現(xiàn)在指的是我正在調(diào)用該函數(shù)的數(shù)組。


現(xiàn)在,實際的實現(xiàn)當(dāng)然比這更有效、更安全、更詳細,但我希望我已經(jīng)回答了你關(guān)于幕后大致發(fā)生的事情的問題。


查看完整回答
反對 回復(fù) 2021-08-20
?
九州編程

TA貢獻1785條經(jīng)驗 獲得超4個贊

正如丹尼爾在評論中指出的那樣,您的函數(shù)是一個回調(diào)函數(shù)(因為不要調(diào)用 use 我們稍后會調(diào)用您)。


在引擎蓋下,數(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

}

它的要點是您提供一個函數(shù)(函數(shù)也是對象)作為參數(shù)。然后 filter 的過濾器實現(xiàn)將使用您的函數(shù)來確定它是否應(yīng)該保留一個值或?qū)⑵鋪G棄。


在Array.prototype你每次創(chuàng)建新陣列只是手段[],將有filter功能。盡管強烈討論,您也可以將自己的函數(shù)添加到數(shù)組中。有時,dump如果您鏈接許多地圖和過濾器,我會得到一個幫助來理解中間值。


查看完整回答
反對 回復(fù) 2021-08-20
  • 2 回答
  • 0 關(guān)注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號