I've see that you can do (e) -> {...} or (event) -> {...} as an input,
But I've also heard you can use any variable for event information like "a", "b", or anything like that. ex. (a) -> {...}
I was under the assumption "e" and "event" were unique, but it sounds like that's not the case from what I've read.
So my question is if I do (a) -> {...} how does the function know "a" contains information about the event? (And why would "a" have information about the event anyway, without being defined or something?)
Thanks, Will
CodePudding user response:
Values are assigned to function arguments, when the function is called, in order. The names you give you them inside the function are irrelevant and unknowable to anything outside the function.
const a = (one, two) => console.log(one, two);
const b = (two, one) => console.log(one, two);
a(1,2);
b(1,2);
The event object is simply passed as the first argument to the function when it is called.
CodePudding user response:
When you define a function in a statement or expression (e) => {...}
you are defining its parameters In this case e
. Parameters are variable references, and they will be undefined until you call the function. When the function is invoked, either in your code or in someone else's, the function is passed an argument which should contain or point to the actual value the caller wants to pass to the function.
So, in your case you are defining a function with a parameter that you are expecting to be a event object. Your function or handler is then invoked by the system, with the event object as an argument, when the event occurs.
The fact that JavaScript is dynamically typed can make this more confusing. Because you don't have to tell JavaScript what type you are expecting for your parameters, and it doesn't care what type (or number) of arguments are passed. You just have to know what you are expecting, and hope that the caller of your function gives you what your are expecting. (or implement your own type checking/assertions)