How is a closure different from a callback?

Check the introduction in this: jibbering.com/faq/faq_notes/closures.html . It can help you understand better how closures relate to functions.

Up vote 7 down vote favorite 2 share g+ share fb share tw.

I asked a question about callbacks and arrived at another question (see comment). How is a closure different from a callback? Javascript closures callback link|improve this question edited Mar 5 '09 at 18:24Patrick McElhaney14.5k95088 asked Mar 5 '09 at 18:15leeand003,30732771 75% accept rate.

Check my edited answer, added a more direct answer on how they relate – eglasius Mar 5 '09 at 18:36.

Check the introduction in this: 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: javascriptkit.com/javatutors/closures2.s... 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.

Ah yes, that clears it up Mr. Fred. Thank you! :) – leeand00 Mar 5 '09 at 19:42.

There's a good definition of closures here: A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression). In practice, that means it's a function that has some hidden variables. A callback is a higher-level idea.

Generally it is a function which is passed around with the intent of being called at a later time. In JavaScript, closures are often used as callbacks.

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.

A callback depending on a context variable aka bound variables (== object state) will be a closure. It will be a pure function, otherwise, when it only takes free variables (== parameters).

A closure carries parts of a local state into a function of some sort, think of it as passing by reference. A callback is meant to notify you about certain change and it redirects program flow. The closure could modify local state but you would never get processor time to handle that, like you would with a callback.

Someone told me the two are related here: stackoverflow.com/questions/615642/… – leeand00 Mar 5 '09 at 18:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions