I think everybody who once coded in Javascript knows the events. As an example I have:
document.addEventListener("keydown", function(event) {
console.log(event);
});
But my head still doesn’t get it: How does the function knows that it has to give back, because I didn’t defint it anywhere. Is it just, that the "addEventListener" says that there hast du be an event in the function, no matter the name or is it about the "keypress"?
5
Answers
A JavaScript function is an object of type
Function
. When you create an anonymous function like that, you are in fact creating a newFunction
object, with parameters and a body. Then, you provide this object as a parameter to thedocument.addEventListener
function, which in turn provides a value to the first parameter of your function, regardless of the parameter name, and executes it. Your function doesn’t know anything aboutdocument.addEventListener
(although you can get its name in your function), it just runs with the value it was provided.The
addEventListener()
is a function that takes two parameters: a string indicating the type of event to listen to, and a listener, which is an object that will receive the event.The name of the listener does not matter and can be a variable to store the event, but it is most common to use a function.
In your example, "event" is the parameter of the function identified as the listener, the parameter of the function (if you provide one) will contain the event and can be named as you please (such as "pastry" in your example).
You can learn more about the
addEventListener()
on the mozilla documentation"addEventListener" provides the event to the function, by means of a regular function call.
Function parameters in JavaScript are usually positional, not name-based. So it doesn’t matter what name the called function uses; addEventListener only cares that it needs to pass the event as parameter #1, and the function will receive it as parameter #1. The "inside" and "outside" variable names might happen to match, or they might not.
For example, when you have
function greet(name)
andfunction greet(pancake)
, an outside caller cannot distinguish between the two: it just doesgreet("Fred")
– it does not specify the name bygreet(name="Fred")
or anything like that.(Some other programming languages do have named parameters – e.g. Python does.)
Since your event callback is still a regular function (and is called like a regular function), the same applies to its parameters.
The second parameter of addEventListener is a function of a specific kind: a function that accepts a parameter of type Event (really of type Object with some fields)
Maybe this is more clear:
A function is a closed piece of script that executes and returns some value, it can receive values to process, called parameters. Parameters can have any name, the name is just a placeholder, a way to identify the value.
In JavasScript you can pass a whole function as parameter and it can be called later. In your example you passed to addEventListener an anonymous function, a function with no name, defined just for the porpouse: "call my function when an event occurs".
In my example I gave that same function a name and a clear definition.
It needs one parameter and, whatever the name of that parameter, it is expected to be an object (group of key value things) with some special fields like "code", "target", etc that you can use.
The function you provide to
addEventListener
is a callback with a specific signature. From that link:So, the parameter name is not relevant (it can be anything from
e
tohaveYourCakeAndEatIt
). The type of the parameter is relevant: it is always an Event.