How do you get the parameter?

2

When I have the following:

var anyn = [1,2,3,4,5,6,7,8,9,10];

function hacerAlgo(elemento) {

console.log(elemento)

}

anyn.forEach(hacerAlgo);

I know it works like this, but I want to know why and how this is called, I would like more examples and documentation, I mean the part of:

anyn.forEach(hacerAlgo);

As far as I know, a function to receive a parameter must be called in this case:

hacerAlgo(parametro); 

but here you never do that .. then?

According to what I've researched, I think it works as just a reference , so the forEach sends each of the elements of the array and the function hacerAlgo , is like the reference to which you will pass by parameters, but I would like more examples apart from forEach , and if I am right or not.

    
asked by Eduardo Sebastian 25.05.2017 в 04:48
source

3 answers

1

The forEach is a method that applies to fixes. It allows to iterate the elements of an arrangement.

For more information you can consult:

link

You are partially correct, it is a function that applies to the array. In that sense, it receives parameters.

All objects in JavaScript come from Object; all objects inherit Object.prototype methods and properties, although they may be overloaded (except for an Object with a null property, eg Object.create (null)). For example, other constructor prototypes overload the constructor property and provide their own toString () method. Changes made to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are further overloaded along the prototype chain.

Greetings,

    
answered by 25.05.2017 в 04:59
1

Just as the documentation is indicated by the ownership of an Object .forEach of type Array does the following:

arr.forEach(function callback(currentValue, index, array) {

});

Where:

  

currentValue       The current element being processed in the array.

     

index   The index of the current element being processed in the array.

     

array   The arrangement in which forEach is being applied.

     

callback   Function to execute for each element, which receives three arguments

  • Array: var anyn = [1,2,3,4,5,6,7,8,9,10]
  • Index: Index of the current element of your array (0,1,2,3 ....) up to the legth-1 of your collection.
  • currentValue: the value of the element in iteration anyn[index] ex: anyn[0] = 1 .

Then this:

var anyn = [1,2,3,4,5,6,7,8,9,10];

function hacerAlgo(elemento) {

console.log(elemento)

}

anyn.forEach(hacerAlgo);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

It's the same as this:

var anyn = [1,2,3,4,5,6,7,8,9,10];

function hacerAlgo(elemento, index, array) {

console.log(array[index])

}

anyn.forEach(hacerAlgo);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Meaning that by default and without the need to specify the parameters that the function expects, it interprets them in the same way.

    
answered by 25.05.2017 в 05:44
0

If a foreach definition were to be made, it could be done with a code like this:

var array = {
    forEach : function (callback){
      for (let i = 0; i < this.elements.length; i++){
          callback(this.elements[i], i, this.elements);
      }
    },
    elements : [],
    set : function (theArray){
      this.elements = theArray;
    }
};

//console.log(array);

// definir array
var miArray = [1,2,3,4,5];

// esto lo hago para que el ejemplo sea similar :P 
// pero imagina que aquí se hace lo mismo que arriba :)
array.set([1,2,3,4,5]);

// llamar foreach
array.forEach(function (element){ 
    element += 1;
});

miArray.forEach(function (element){ 
    element += 1; 
});

// esto sucede por que se pasan por valor
console.log("ejemplo 1", array.elements, miArray);

// para que funciones debes usar el tercer parametro
// llamar foreach
array.forEach(function (currentValue, index, arr){ 
    arr[index] += 1;
});

miArray.forEach(function (currentValue, index, arr){ 
    arr[index] += 1;
});

// ahora si funciona
console.log("ejemplo 2", array.elements, miArray);

The foreach is a method of arrays, which works like the example I have attached to you (I would dare to say the same). The difference is that when you do it with brackets "[]" you create an own object of the language and you call native methods implemented by the web browser (or the interpreter of js you use).

These types of functions are called callbacks or callbacks and exist in most of the programming languages.

  

A callback or callback is an "A" function that is used as an argument to another "B" function. When "B" is called, it executes "A". To achieve this, usually what is passed to "B" is the pointer to "A".

Additionally, the explanation of why "example 1" does not work is because the values of the array that are sent to the callback are made as values and not as references (because they are primitive). This means that the "original" value is not modified but a temporary copy that the language creates for the scope of the function is modified. You can see this in the function documentation :

  

Primitive parameters (such as a number) are passed to functions by value; The value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

    
answered by 25.05.2017 в 05:50