Why pass Event in a function as a parameter?

Asked

Viewed 185 times

3

I have this doubt for a long time, but I’m posting it now. I’ve seen several times in codes the programmers use as a parameter in functions event and I always wonder if it has any relevance in using it as a parameter because in the end the result will always be the same.

In the example below by clicking on the first paragraph the default action of the contextmenu which is to display the default browser context menu and when you click the second paragraph the default action of contextmenu:

Example:

<p>Parágrafo 1</p>
<p id="p2">Parágrafo 2</p>
  
<script>
    let p2 = document.querySelector("#p2");

    p2.addEventListener("contextmenu", function(event) {
        event.preventDefault();  
    });
</script>

But if I remove the event the code will work I say this because I saw no difference occur:

<p>Parágrafo 1</p>
<p id="p2">Parágrafo 2</p>

<script>
    let p2 = document.querySelector("#p2");

    p2.addEventListener("contextmenu", function() {
        event.preventDefault();  
    });
</script>

I’m not referring to what is the event is why to use the event in the function? parameters or in the above examples that is commonly found in third party codes?

  • It only makes sense to have Event if you’re gonna use it, or you don’t have to. In this case, as said you know what is Event, he is canceling the action of this event, but if you do not need to receive him in function

  • But then at any time I won’t need to pass it on the function since it works without?

2 answers

1


Modern browsers (the current ones, including the infamous IE11) have incorporated the event as native property of the object window. If you open the console and run event will return undefined, that is, it exists. If it did not exist, it would return:

Event is not defined

Actually nowadays it seems no longer necessary to use the event as a parameter (I say "looks" because I haven’t tested in all current browsers), but I think it is recommended to always use, because browsers like Firefox a little while ago (less than 2 years, like shows this topic) did not possess the event native, and if you did not put it, it would result in the above error.

However, this parameter does not necessarily need to have the name event, i.e., you can name it with any valid name, it is only a variable that receives the object of the event fired. For example, it could be evento, e, ev, a, b etc., as you find best in the context of your code:

let p2 = document.querySelector("#p2");
p2.addEventListener("contextmenu", function(qualquercoisa) {
   qualquercoisa.preventDefault();  
});
<p>Parágrafo 1</p>
<p id="p2">Parágrafo 2</p>

  • Thank you! @sam valuable reply.

  • Thank You Young! Abs!

0

The parameter Event is not mandatory, but often it may be necessary. What you have within this parameter are data related to browser events, for example something related to the type of event that was fired, browser screen size, position from where it was clicked in the browser, etc. Generally speaking, I hope to have helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.