I asked a question about callbacks and arrived at another question (see comment). How is a closure different from a callback?
a callback is executable code that is passed as an argument to other code. a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. a callback is executable code that is passed as an argument to other code.
Callbacks are also closures as the passed function is executed inside other function just as if the callback were defined in the containing function.
Callback is for asking another function to do business operations and send a result whereas events are not but asking for handover the control so that we can only handle business operation. Eg: Button click is event (We are doing business operation on click of button).
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.
Check the introduction in this: http://jibbering.com/faq/faq_notes/closures.html. It can help you understand better how closures relate to functions.
Here is a set of closure examples: http://www.javascriptkit.com/javatutors/closures2.shtml
Basically, the callback is like a function pointer. The bit that makes it a closure, is when that function accesses anything on the context where it lives, like variables outside it. When that happens, the function will use the current values of the variables (as opposed to copy them). See example 4.
Different definitions:
Callback -
a callback is executable code that is passed as an argument to other code.
Closure -
a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With