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
– Ricardo Pontual
But then at any time I won’t need to pass it on the function since it works without?
– marquinho