admin管理员组文章数量:1334150
I am trying to bee a little bit familiar with the JavaScript terminology. I think I am confused with the terms iteratee, callback and function factory
Let me use the following silly example:
//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]
So, to my understanding thus far:
- iteratee (aka predicate) is a function object that does some work. In this example
mapForEach
accept afunc
object to do some work with thearr
elements, sofunc
can be called iteratee and hencemultiplyBy
is an iteratee. In a general concept maybemultiply
can also be considered an iteratee as is a standalone function that performs a basic work. - callback is a function A you give to another function B to be invoked by the other function (so, the other function B --sort to speak-- "calls back" the function A). In this example when
mapForEach
is called, is executed in a new environment where it calls back thefunc
function object. So thefunc
object can also be called a callback. However, in the same context,mapForEach
can also be considered as a function that, when is executed, calls back themultiplyBy
function, i.e.multiplyBy
is a callback too. - function factory we call a function that does some work for us and/or return a value (i.e an array, object, function object, whatever....). In our example
multiplyBy
is a function object that when it is called it returns (a copy of) another function object (multiply
) that has themultiplier
parameter in its closure. ThusmultiplyBy
is a factory function.
Am I getting all these right, (or am I going insane :-P)
I am trying to bee a little bit familiar with the JavaScript terminology. I think I am confused with the terms iteratee, callback and function factory
Let me use the following silly example:
//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]
So, to my understanding thus far:
- iteratee (aka predicate) is a function object that does some work. In this example
mapForEach
accept afunc
object to do some work with thearr
elements, sofunc
can be called iteratee and hencemultiplyBy
is an iteratee. In a general concept maybemultiply
can also be considered an iteratee as is a standalone function that performs a basic work. - callback is a function A you give to another function B to be invoked by the other function (so, the other function B --sort to speak-- "calls back" the function A). In this example when
mapForEach
is called, is executed in a new environment where it calls back thefunc
function object. So thefunc
object can also be called a callback. However, in the same context,mapForEach
can also be considered as a function that, when is executed, calls back themultiplyBy
function, i.e.multiplyBy
is a callback too. - function factory we call a function that does some work for us and/or return a value (i.e an array, object, function object, whatever....). In our example
multiplyBy
is a function object that when it is called it returns (a copy of) another function object (multiply
) that has themultiplier
parameter in its closure. ThusmultiplyBy
is a factory function.
Am I getting all these right, (or am I going insane :-P)
Share Improve this question edited May 21, 2016 at 18:59 iraklisg asked May 21, 2016 at 17:05 iraklisgiraklisg 5,7996 gold badges31 silver badges41 bronze badges1 Answer
Reset to default 9Iteratee is not a function that just does some work. It has to do some work on an iterable set, an array for example. A predicate is a function that takes an argument and returns true/false, predicates are for example used for filtering iterable sets. Thus, iteratee and predicate are definitely not the same.
Function factory is not just a function that does some work and returns a value. It is a function that is capable of creating a family of other functions based on supplied arguments.
The multiplyBy
is not a callback "per se", the func
passed to mapForEach
is. The multiplyBy
bees a callback when you pass it to another method that calls it back.
本文标签:
版权声明:本文标题:Understanding the terms iteratee, callback, function factory in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742341575a2456708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论