What would be the 'e' passed as parameter in js functions?

13

What would be the 'e' that is passed as parameter in functions? Ex.:

function nome(e) {
    (instrução);
}

Does anyone have any material, or keyword for research that I can use to study a little more about it?

    
asked by anonymous 04.05.2015 / 15:07

2 answers

20

It is common to name e as an abbreviation of event , to pass a reference to the Event object used in callback functions of event dropper. Shorten for convenience and to save bytes.

The Event object itself is useful for eg:

etc ...

    
04.05.2015 / 15:10
3

In this case this 'e' is a specific parameter in specific functions, such as events.

It's very common in jQuery. For example, when we create an event of onclick on a link:

$('#link-legal').on('click', function(e) {
    alert('Link legal clicado!');
});

In this case, this 'e' parameter (which can actually have any name, but the 'e' letter short for 'event' is used) will contain the clicked object with several properties and methods. / p>

Application example

Let's say that you want a confirmation to the user to be clicked on a link and, depending on the response, the click is 'canceled':

$('a.confirmar').on('click', function(e) {
    var confirmado = confirm('Deseja realmente prosseguir para esse link?');
    if(confirmado == false) {
        e.preventDefault();
        alert('Click cancelado');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="https://www.google.com" target="_blank" class="confirmar">Ir para o Google</a>

In this example we use the preventDefault method that cancels the default action of the element, which in the case of the <a> tag is the targeting of the url indicated in the href property.

Generally in documentation you find this type of reference, as you can check in the official jQuery documentation for the event click :

link

There it says (in free translation):

  

.click (handler)    handler   Type: Function (Event eventObject)   A function that will execute at each event trigger.

You may notice that in the type it says that a Function in which an object parameter of type Event is passed.

    
30.08.2017 / 22:52