我试图对JavaScript术语有点熟悉。我认为我对术语iteratee,callback和function factory感到困惑
让我使用以下愚蠢的示例:
//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
是。将multiplyBy
成为当你把它传递给调用它后面的另一种方法的回调。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句