There is such an example in javascript:

var fruits = ['apple', 'banana', 'orange', 'grapes', 'pear', 'passionfruit']; // The three values on the callback function are: // element - The element being traversed // index - The current index of the item in the array starting at 0 // array - The array being traversed (probably mostly irrelevant) fruits.forEach(function(element, index, array) { console.log(index, element); }); 

a forEach is passed to the forEach method. I understand that somewhere in the forEach method this passed function is called. To call it, you need to pass 3 arguments to it (preferably). The people who wrote the code for this function somehow sort through the elements of the fruits array and call my callback function, passing all these three parameters to it. How do they know what is in what place? After all, in fact, I can not write

 function(element, index,array) 

but

 function(myelement, myindex,myarray) 

those. change the name of the parameters and will still work. It turns out that the elements passed to the function are "tied" to the place of the parameter - 1, 2, 3 in the account. So? Or somewhere I'm wrong. Tell me please?

I will carry the question separately:

are the parameters passed to the callback function "tied" to the place of the parameter?

    3 answers 3

    The arguments passed to the function are essentially an array of arguments[] . Your suggestion that order matters is true.

    The magic of JavaScript: arguments (Habr).

    • strictly speaking, arguments are not an arraymymedia
    • @mymedia write about this? - Nick Volynkin

    The people who wrote the code for this function somehow sort through the elements of the fruits array and call my callback function, passing all these three parameters to it. How do they know what is in what place?

    Everything is very simple - they do not know how the function is described, which is transmitted as a callback.

    They just take it and call it passing parameters: element, index, sourse_array.

    In addition, the callback context can be specified ( this value) inside the function.

    Since the forEach developers do not know what function they will be given, they simply call any callback in the following form:

     callback(element,index,sourse_array) 

    Or additionally setting this , which is equivalent to calling a call

     callback.call(thisArg,element,index,sourse_array) 

    In this case, the callback function itself can be declared with three parameters, with two, with four, with no parameters at all, etc.

    More description forEach in the help

      You can name the arguments as you like, for example

       fruits.forEach(function(first, second, third) { // first всегда будет итерируемым елементом масива // second всегда будет его номером // third всегда будет масивом по которому осуществляется итерация // например даст true console.log(third[second] === first) }); 

      In addition, all arguments are optional (although forEach loses its meaning without the first element)

      More details can be read here . There is also an example of the implementation of the function forEach() at the end.

      • one
        Thank! The implementation is written just such an approach. - Olga