-1

So I'm trying to understand functional programming and higher order functions in specific. What I never clearly understood, was how callback function's parameters have a connection to, for example, an array?

To be more clear, let's take this code example:

var friends = ["Mike", "Stacy", "Andy", "Rick"];

friends.forEach(function (eachName, index){
console.log(index + 1 + ". " + eachName); // 1. Mike, 2. Stacy, 3. Andy, 4. Rick
});

How a callback parameter eachName knows that it means to return the item from array and index is supposed to return an index of array? How the connection between an array and callback parameter works?

Limpuls
  • 856
  • 1
  • 19
  • 37

2 Answers2

2

the forEach method is calling or invoking your callback function. To be more specific let's see how the forEach work.

function forEach(arr, func) {
  for (var i = 0; i < arr.length; i++) {
    func(arr[i], i);
  }
}

Now you can do something like this.

function forEach(arr, func) {
  for (var i = 0; i < arr.length; i++) {
    func(arr[i], i);
  }
}

var cars = ["BMW", "Tata Nano", "Lamborghini"];

forEach(cars, function (car, index) {
  alert(car + " "  + index);
});
  • 3
    [Insert caveat about iterating over arrays using `for..in` here…] – deceze May 15 '17 at 01:09
  • We can also use for in loop for an array because array is basically object in which all the indexes are stored according to numbers. –  May 15 '17 at 01:11
  • 2
    Okay, okay, I'll insert the actual caveat: [Why is using “for…in” with array iteration a bad idea?](http://stackoverflow.com/q/500504/476) – deceze May 15 '17 at 01:13
  • Now I understand. And thanks for clearing things out about `for..in`. I'm just wondering, in the `func(arr[i], i);` second parameter `i` works as iterator for index of the element in array because we used the variable in `arr[i]`? How it knows that it's supposed to be an index when we call the function? – Limpuls May 15 '17 at 01:25
  • i don't under stand your question. Can you be more clear –  May 15 '17 at 01:28
  • Never mind, I figured it out myself, that `i` is defined in the for loop body as 0 and is incremented by one each time and so is used for indexing elements in array? So if `i` variable is used as `arr[i]`, we get each item, but if we just use parameter `i`, it's the same as just typing [0], [1], etc and we get the index only. Am I right? That's what I wanted to understand. – Limpuls May 15 '17 at 01:31
0

The API does it for you. In this case, array.forEach's callback is always set to accept 3 arguments: the current item, the current item's index in the array, and the array itself.

One example of an API that has a different argument signature is array.reduce. It is set to accept 4 arguments: The accumulator, the current item, the current item's index and the array itself.

Joseph
  • 117,725
  • 30
  • 181
  • 234
  • Oh, I never knew that is just the API that is so designed. I thought you can throw whatever parameters you want and that's why I was wondering how it works after that. But if it's programmed in to take 3 arguments and those arguments are fixed, then it makes sense. Could you link me to an article or something for more information on this? Thanks! – Limpuls May 15 '17 at 01:05
  • 1
    @Limpuls MDN is a good resource. Standard function rules apply. For instance, `function.bind` can alter the context and prepend fixed arguments. Arrow functions also apply their "this-less" nature. – Joseph May 15 '17 at 01:42
  • So if we want to use our own custom made higher order function and not built-in one, like `forEach`, we can decide how many parameters it can have and what they will be (like I said in my prev post, program them in and make fixed, like first argument will be accumulator, second currentIndex and so on) by using `.bind`? – Limpuls May 15 '17 at 01:53