From this answer (What is a handler) I understand that a handler is:
A handler is a routine/function/method which is specialized in a certain type of data or focused on certain special tasks.
But then I'm asking myself: why not replace the word handler with the word function? What's the difference? The only difference I can tell is that functions do not need to have arguments, which means they do not need to be focused on a certain type of data. And some functions -- without arguments -- are not focused on specialized tasks.
But other than that, what is the difference between a handler and a function?
Some more background:
I find the concept of handler difficult to understand still (it could be because I'm Dutch and there is not a word for it in Dutch according to Google Translate). Checking out the definition helps a bit but not fully.
From this answer (What is a handler) I understand that a handler is:
A handler is a routine/function/method which is specialized in a certain type of data or focused on certain special tasks.
But then I'm asking myself: why not replace the word handler with the word function? What's the difference? The only difference I can tell is that functions do not need to have arguments, which means they do not need to be focused on a certain type of data. And some functions -- without arguments -- are not focused on specialized tasks.
But other than that, what is the difference between a handler and a function?
Some more background:
I find the concept of handler difficult to understand still (it could be because I'm Dutch and there is not a word for it in Dutch according to Google Translate). Checking out the definition helps a bit but not fully.
Share Improve this question asked May 31, 2017 at 10:00 Melvin RoestMelvin Roest 1,4921 gold badge18 silver badges33 bronze badges 6- 5 A function is a function. A handler is a function that runs when an event fires. Edit: I see that the linked question mentions other types of handlers, but 99% of the time I use "handler" for "event handler". – user5734311 Commented May 31, 2017 at 10:01
- "why not replace the word handler with the word function" — Why talk about cars instead of vehicles? Why talk about dogs instead of animals? Why talk about animals instead of collections of atoms? – Quentin Commented May 31, 2017 at 10:06
- An ordinary function is something you invoke. A handler (or callback) is something someone else invokes. – georg Commented May 31, 2017 at 10:11
- Quentin, good point. I didn't see it as clearly, hence the (slightly) provocative question. Especially the cars instead of vehicles part makes it clear to me. It also makes it clear that I don't understand the difference well enough. – Melvin Roest Commented May 31, 2017 at 10:12
- 1 Just don't ask about the difference between an "event listener" and an "event handler". – Alohci Commented May 31, 2017 at 10:14
4 Answers
Reset to default 11A handler is simply a more specific term. What's the difference between a fruit and an apple?
All handlers in JS are functions, but not all functions are handlers. It's a way of being more precise in your terms. If I say "a function", I could be referring to any function. If I say "a handler", then I'm referring specifically to a function which is intended to respond to some event that is occurring, usually asynchronously like a mouse click or in Express a request being processed.
Handlers are indeed just functions. Handlers are functions that have the intended behaviour of being called right after an event is fired, for example when an img
is clicked or when you scroll past a certain element on the page.
Consider this: You have a button in a web page, then you press the button, you will have the button HANDLER fires, but a handler is non other than a function that is used to handles an event.
Imagine an onclick event in html, it will call a function, that function is the handler.
Handlers are functions that are called in response to some sort of actions/events whereas, a function is what we call from our code.
In most cases, a handler receives an argument describing what sort of event fired it.
Generally, It is just a function but the cause for which it is called makes it different.