我们如何理解和创建javascript函数中的这个参数?
我是JS语言的新手。我对用函数创建的变量声明有很多困惑。通常情况下,您会按如下方式创建和调用:我们如何理解和创建javascript函数中的这个参数?,javascript,Javascript,我是JS语言的新手。我对用函数创建的变量声明有很多困惑。通常情况下,您会按如下方式创建和调用: function sum(a, b) { return a + b } 我们知道a,b可以在以后用值来调用,这一点毫无疑问 console.log(sum(1, 2)) 但我对内置函数中传递的参数和一般情况有疑问。它们是: var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
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);
匿名函数中的这个word参数如何掌握数组中的每个元素?这背后的概念是什么
不仅在这个内置函数中,这也是我对Javascript的普遍怀疑。每次都让我困惑。请有人告诉我如何创建和有效地使用这种参数 正如Daniel在评论中指出的,您的函数是一个回调函数(正如在“不要调用使用”中一样,我们稍后将调用您) 在引擎盖下,阵列过滤器功能实现如下:
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
}
Array.prototype.filter=函数(回调){
常数goodValues=[]
for(设i=0;i
其要点是提供一个函数(函数也是对象)作为参数。然后filter的filter实现将使用您的函数来确定它是应该保留一个值还是将其丢弃
Array.prototype
仅仅意味着每次创建一个新的数组[]
时,它都将具有过滤器
功能。尽管受到了强烈的讨论,但您也可以将自己的函数添加到数组中。有时我有一个dump
助手来理解中间值,如果你链接了许多映射和过滤器
根据我的理解,这里有一个,您对anonymous/lambda函数如何在数量不定的参数上运行感到困惑 让我们编写自己的过滤器函数,看看它是如何工作的。首先,我们需要两样东西,一个对象/值的数组,以及将这些值映射为true(应该在结果数组中)或false(不应该在结果数组中)的某种方法。然后,我们所需要做的就是选择要计算的值并返回它们
// 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' ]
请注意,我们得到的结果与之前相同。JavaScript不仅允许我们将过滤器作为输入参数传递,还允许我们无缝地调用它。但是,我们如何将过滤器直接应用于单词数组本身呢?这就是原型派上用场的地方,因为它允许我更改现有类的“基本”代码。例如,Array.prototype
允许我访问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' ]
函数过滤器OrdsPrototype(过滤器){
filteredWords=[];
对于(var index=0;index
这里没有魔法。最后一个words.filterWords(hasAnE)
看起来非常接近原始的words.filter(someFunction)
请注意,我是如何去掉数组参数并将数组的所有引用改为this
。这是因为我将函数设置为数组本身(以及所有数组)的方法,所以这个现在指的是我正在调用函数的数组
当然,实际的实现要比这个高效、安全、冗长得多,但我希望我已经回答了你关于引擎盖下面发生的事情的问题。这叫做回调函数。.filter
调用回调。@mithun jack:我不能保证你为什么被否决。对于这个问题:filter函数就是这样实现的。当调用一个数组时,过滤器迭代每个元素,并将该元素传递给函数(回调函数),并返回一个新数组作为函数操作的结果。这个链接可能会让你更清楚@我说我一般都问这个问题。“不仅在这个内置函数中,这也是我对Javascript的普遍怀疑”。过滤器只是我从Mozilla开发者网站上取的一个例子。使用const I=0
,I++
会抛出一个错误。filter
函数实际上还执行多个定义的步骤。您关于const的权利修复了该示例。以上并不是规范的正确实现,而是一个简单的例子来帮助PO理解发生了什么。
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' ]