繁体   English   中英

我们如何在javascript函数内部理解和创建此参数?

[英]How can we understand and create this parameter inside function in javascript?

我是JS语言的新手。 我对使用函数创建的变量声明有很多困惑。 在创建和调用时,通常是这样的:

function sum(a, b) {
    return a + b
}

我们知道a,b可以稍后通过值调用,这无疑是

console.log(sum(1, 2))

但是我对内置函数和一般传递的参数有疑问。 他们是:

   var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(function (word) { return word.length > 6} );

console.log(result);

匿名函数中的单词参数如何把握数组中的每个元素? 其背后的概念是什么?

不仅在此内置函数中,这通常是我对Javascript的怀疑。 每次让我感到困惑。 请有人向我解释如何有效地创建和使用这种参数。

据我了解,您对匿名/ lambda函数如何对不确定数量的参数进行操作感到困惑。

让我们编写我们自己的过滤器函数以查看其工作原理。 首先,我们需要两件事,对象/值的数组,以及将这些值映射为true(应在results数组中)或false(不应在results数组中)的某种方式。 然后,我们要做的就是选择评估后的值,然后尝试将其返回。

// 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' ]

好的,所以我们可以自己构建一个过滤器功能,但是如何进入words.filter(hasAnE)呢? 首先,您需要了解的是,在JavaScript中,您可以像变量一样传递函数本身。 这意味着我们可以编写一个将另一个函数作为参数的函数(传递的函数称为“回调”)。

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' ]

注意,我们得到的结果与以前相同。 JavaScript使我们不仅可以将过滤器作为输入参数传递,还可以无缝调用它。 但是我们如何将过滤器直接应用于单词数组本身呢? 这是原型派上用场的地方,因为它使我可以更改现有类的“基本”代码。 例如, Array.prototype使我可以访问Array类的所有默认字段和方法(我们的词array是其中之一)。 因此,利用我们可以将函数存储为变量的知识,我们可以这样做:

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)非常接近words.filter(someFunction)请注意我是如何摆脱array参数并将所有对数组的引用更改为this 那是因为我将函数设置为数组本身(以及所有数组)的方法,所以现在this是指我正在调用函数的数组。

当然,现在实际的实现比这要高效,安全和冗长得多,但是我希望我已经回答了您有关引擎盖底层情况的问题。

正如Daniel在评论中指出的那样,您的函数是一个回调函数(如不调用use,我们将在以后调用您)。

在内部,数组过滤器功能实现如下:

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
}

其要点是您提供一个函数(函数也是对象)作为参数。 然后filter的filter实现将使用您的函数来确定是保留值还是将其丢弃。

Array.prototype只是意味着每次创建新数组[] ,它将具有filter功能。 尽管强烈论述,您也可以将自己的函数添加到数组中。 有时,如果您链​​接许多地图和过滤器,我会有一个dump助手来了解中间值。

这是一个演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM