What is the function of this 'e' that is passed as a parameter?

8

The example I'm going to show here is just to illustrate.

I would like to really understand how this e works, which is usually passed as a parameter in several functions, that encounters in codes out there.

 $(".fotos").each(function(e){
                    if(e <= e + 2){
                        teste[posicao][e] = e;

                        if((e+1)%3 == 0){
                            posicao +=1;
                            teste[posicao] = new Array();
                        }

                    }
    
asked by anonymous 25.09.2015 / 16:10

3 answers

6

When you use the jQuery API to $(".fotos").each this method accepts a function as an argument. This function is also called iterator , that is the function that will be called and run for each of the $(".fotos") found by that selector.

The variable in your example is called e but could be another name. What is important is what it contains. And in the case of jQuery it is the index (index / position) of this element that is being iterated in the array, type: Number .

If this function had two arguments the second would be the value itself, and it will be the type of what is inside the array and to be iterated, an element with class .foto in your example.

Note that jQuery does this unlike the native JavaScript API, which makes the value available to the first, and then the index.

But the (e) is this only in all cases of functions there on the web?

Well, you can not generalize like this. However the variable e is often used as an abbreviation for "element" or even "event".

In your example, it should not even be e , semantically, but i . If there were two arguments I would write .each(function(i, e){ as abbreviations of .each(function(index, elemento){ .

If it were another type of function, such as a callback of an event dropper, e would be an event abbreviation. For example:

$('div.teste').on('click', function(e){
    e.preventDefault(); // aqui "e" é abreviatura de evento e contem o objeto evento
    
25.09.2015 / 16:19
4

This is a parameter. An argument will be passed to it. I imagine that even though I do not know the correct terminology, I know what a parameter is for.

In this case, the e will receive arguments sent by the each() function. This function is intended to scan a collection of data. Then each member of this collection will call the anonymous function written there and the collection element (in this case, the index it will be sent as an argument.

I am actually describing what I know of this function. Functions that call anonymous functions should document how the anonymous function (what you wrote) should be written, what parameters it should receive, in general what it should do and what it should return.

And of course you can write a function of your that receives an anonymous function as an argument. If you do this, you have to document how it will be used.

Let's see the source of the each() function:

function (obj, callback) {
    var length, i = 0;

    if (isArrayLike(obj)) {
        length = obj.length;
        for (; i < length; i++) {
            if (callback.call(obj[i], i, obj[i]) === false) {
                break;
            }
        }
    } else {
        for (i in obj) {
            if (callback.call(obj[i], i, obj[i]) === false) {
                break;
            }
        }
    }

    return obj;
}

The callback.call makes the call to their function. The parameters of this function are:

  • thisArg - which is the element
  • arg1 and arg2 - which is the index and again the element

The arg1 is actually passed as an argument to your anonymous function. In case it is expressed by the variable i in the loop.

If your function declares the parameters as (i, e) you can receive the index and the element being parsed in that iteration. In some situations just receive the index, in others also need to know the exact element.

A final addendum: almost always a common loop solves as well or better than using the each() function.

    
25.09.2015 / 16:19
3

Let's unmount the function call:

$(".fotos").each(function(e) {
  • $(".fotos") is a selector of jQuery. The target can be a member only, or a collection;
  • .each is a method that allows a function call to all members of the collection returned by the selector;
  • function(e) is the function called by each method . The and parameter, in this case, references the index of the selected element.

So, if your selector returns a collection containing 3 elements, the function will be called 3 times, the value of and being the index value. >

The and name is arbitrary. It could be any valid name, as long as the code that consumes it makes the correct reference:

$(".fotos").each(function(parm1){
            if(parm1 <= parm1 + 2){
                teste[posicao][parm1] = parm1;
    
25.09.2015 / 16:16