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 a func object to do some work with the arr elements, so func can be called iteratee and hence multiplyBy is an iteratee. In a general concept maybe multiply 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 the func function object. So the func 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 the multiplyBy 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 the multiplier parameter in its closure. Thus multiplyBy 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 a func object to do some work with the arr elements, so func can be called iteratee and hence multiplyBy is an iteratee. In a general concept maybe multiply 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 the func function object. So the func 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 the multiplyBy 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 the multiplier parameter in its closure. Thus multiplyBy 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 badges
Add a ment  | 

1 Answer 1

Reset to default 9

Iteratee 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.

本文标签: