Is there any difference between an event with passing parameters to one without a pass?

5

Considering:

jQuery

$("#seletor1").on("click", function(){
    $("#seletor2").trigger("click", {dados: "informação"});
});
$("#seletor2").on("click", function(){
    //faça coisas
});
$("#seletor2").on("click", function(evento, dados){
    //faça coisas
});

What will it do?

    
asked by anonymous 15.07.2014 / 22:56

1 answer

6

Both will be triggered . The passage of the parameters is determined by who issues the event, not by its appointment in the listeners. So the first parameter (event object) is always passed by jQuery, regardless of whether it is named in your listener or not.

In the first case, evento and dados can still be accessed via arguments[0] and arguments[1] respectively:

$("#seletor2").on("click", function(){
    var evento = arguments[0];
    var dados = arguments[1];
});

In your second listener, the variables do not need to be defined because the parameters are already named, but the arguments object is also available:

$("#seletor2").on("click", function(evento, dados){
    console.log(evento === arguments[0]); // true
    console.log(dados === arguments[1]); // true
});
    
15.07.2014 / 23:01