Javascript如何匹配回调函数中的参数? [英] How does Javascript match the parameters in the callback function?
问题描述
我刚开始学习Javascript,回调函数似乎很难理解。我有一个问题是JavaScript如何匹配参数在回调函数?例如以下forEach循环:
I just started to learn Javascript, and callback functions seem hard to understand. One question I have is how javascript matches the parameters in a callback function? for example in the following forEach loop:
var friends = ['Mike', 'Stacy', 'Andy', 'Rick'];
friends.forEach(function(eachName, index){
console.log(index + 1 + ". " + eachName);
});
默认情况下,forEach函数会将索引传递给第二个参数,并输入第一个参数在回调函数中?
Is it by default that the forEach function will pass index to the second parameter and entry to the first parameter in the callback function?
为了掌握回调函数,我需要在每次使用它时检查API(在这种情况下,forEach)吗?
In order to master callback functions, do I need to check the API (in this case, forEach) every time I use it?
推荐答案
默认情况下,forEach函数将通过索引到第二个参数和回调函数中的第一个参数的入口?
Is it by default that the forEach function will pass index to the second parameter and entry to the first parameter in the callback function?
是的; 这是规范的一部分。实际上,它还传递正在迭代的数组作为第三个参数。
Yes; this is part of the specification. In fact, it also passes the array being iterated as the third argument.
使用T调用callbackfn的[[Call]]内部方法作为包含[值],[索引]和[对象] 的此值和参数列表。
Call the [[Call]] internal method of callbackfn with T as the this value and argument list containing [the value], [the index], and [the object].
(强调我的)
为了掌握回调函数,我需要检查API(在这种情况下, forEach)每次我使用它?
In order to master callback functions, do I need to check the API (in this case, forEach) every time I use it?
好吧,他们是相互一致的,所以你会记住在某一点。 map
,过滤器
,每
和一些
也可以这种方式工作。
Well, they’re pretty consistent with each other, so you’ll remember at some point. map
, filter
, every
, and some
also work in this way.
这篇关于Javascript如何匹配回调函数中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!