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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

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

2 回答

?
滄海一幻覺(jué)

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)題。


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

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)理解中間值。


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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