[英]Understanding the terms iteratee, callback, function factory in JavaScript
我试图对JavaScript术语有点熟悉。 我认为我对术语iteratee , 回调和函数工厂感到困惑
让我使用以下愚蠢的例子:
//this function accept an array and then returns a new array that
//contains the elements of the first array altered by func
function mapForEach(arr, func) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
newArr.push(func(arr[i]));
}
return newArr;
}
//this function multiplies two numbers
var multiply = function (a, b) {
console.log(a * b);
};
//this is a silly function that passes a multiplier as the first
//argument of the multiply function
var multiplyBy = function(multiplier) {
return multiply.bind(this, multiplier);
};
var arr = [1, 2, 3];
mapForEach(arr, multiplyBy(3)); //[6, 10, 20]
所以,到目前为止我的理解:
mapForEach
接受一个func
对象来对arr
元素做一些工作,所以func
可以被称为iteratee ,因此multiplyBy
是一个iteratee 。 在一般概念中, 也可以multiply
也视为迭代 ,因为执行基本工作的独立函数也是如此。 mapForEach
时,在新的环境中执行它调用func
函数对象。 因此func
对象也可以称为回调 。 但是,在相同的上下文中, mapForEach
也可以被视为一个函数,在执行时, 它会回调 multiplyBy
函数,即multiplyBy
也是一个回调函数。 multiplyBy
是一个函数对象,当它被调用时,它返回另一个函数对象( multiply
)的副本(在其闭包中具有multiplier
参数)。 因此multiplyBy
是工厂函数 。 我得到了所有这些,(或者我疯了:-P)
Iteratee不是一个只做一些工作的功能。 它必须在可迭代集上做一些工作,例如数组。 谓词是一个接受参数并返回true / false的函数,谓词例如用于过滤可迭代集。 因此,iteratee和谓词绝对不一样。
函数工厂不仅仅是一个工作并返回值的函数。 它是一个能够根据提供的参数创建一系列其他函数的函数。
multiplyBy
不是“本身”的回调func
传递给mapForEach
的func
是。 当您将multiplyBy
传递给另一个调用它的方法时,它将成为一个回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.