0

In Javascript, callback function is always a closure? The inverse is not true [?] as you see bellow:

function makeFunc() {
  var name = "Mozilla";
  function displayName() {
    alert(name);
  }
  return displayName;
};

var myFunc = makeFunc();
myFunc();

Where myfunc is a closure, and not a callback.

Closures are functions that inherit variables from their enclosing environment. When you pass a function callback as an argument to another function that will do I/O, this callback function will be invoked later, and this function will — almost magically — remember the context in which it was declared, along with all the variables available in that context and any parent contexts. This powerful feature is at the heart of Node’s success.

From other link:

Also, because callbacks behave as if they are actually placed inside that function, they are in practice closures: they can access the containing function’s variables and parameters, and even the variables from the global scope.

link

When we pass a callback function as an argument to another function, the callback is executed at some point inside the containing function’s body just as if the callback were defined in the containing function. This means the callback is a closure. Read my post, Understand JavaScript Closures With Ease for more on closures. As we know, closures have access to the containing function’s scope, so the callback function can access the containing functions’ variables, and even the variables from the global scope.

carduh
  • 529
  • 1
  • 4
  • 13
  • 2
    `printLocation` is not a closure in your example. – Quentin Dec 28 '15 at 21:51
  • 5
    I'm afraid it's not clear what you're asking. – Louay Alakkad Dec 28 '15 at 21:51
  • 2
    What do you think a closure is? Are you asking, "When passing a function literal into a function as a argument, does that passed-in function close over its non-free variables?" That's one of *many* ways I could parse your question. – apsillers Dec 28 '15 at 21:51
  • 1
    Maybe start here: http://stackoverflow.com/questions/2070275/javascript-closures-and-callbacks – Roope Dec 28 '15 at 21:51
  • [Good stackoverflow topics include](http://stackoverflow.com/help/on-topic) *a specific programming problem, or a software algorithm, or software tools commonly used by programmers; and is a practical, answerable problem that is unique to software development* — This question, however, seems to be something that doesn't involve any **practical** problem and is likely to end up being about hair splitting over the meaning of terminology. – Quentin Dec 28 '15 at 21:54
  • 1
    @carduh _A callback_ can't use variables declared in a scope it is invoked, only it can use variables in the scope (or outer) it was declared/defined. The basic problem in your question is, that it doens't contain a callback, just a closure. As Quentin stated, this might be "hair splitting over the meaning of terminology", but assuming the returned `displayName` is that you call "callback", is actually a closure, not a callback. c-smile have a great explanation for both terms though. Notice, that a scope is somthing you literally write to a file, it can't be changed dynamically. – Teemu Dec 28 '15 at 22:24
  • Already edited @Louy – carduh Dec 28 '15 at 22:34

1 Answers1

9

callback is always a closure?

These are orthogonal terms.

Callback is a function reference passed to another function to be called from inside it.

Closure is always an inner function that uses variables from outer function. If inner function does not use any outer variables then it "closes nothing" - just a function as any other.

c-smile
  • 26,734
  • 7
  • 59
  • 86
  • If a callback uses variables from outer function (enclosing environment) will became a closure. Right? @c-smile – carduh Dec 28 '15 at 21:59
  • callback is just a function reference. That function can be a closure or not - does not matter. Moreover, having function reference in JS, there is *absolutely no way* to distinguish closuring function from any other. – c-smile Dec 28 '15 at 22:13
  • 1
    Closures are functions that inherit variables from their enclosing environment. When you pass a function callback as an argument to another function that will do I/O, this callback function will be invoked later, and this function will — almost magically — remember the context in which it was declared, along with all the variables available in that context and any parent contexts. FROM: Professional Node.js A callback function passed as argument will remember the context. So if it happens is a closure? Callback function became Closure. This is what I want to confirm @c-smile – carduh Dec 28 '15 at 22:22
  • Question edited with more details. @c-smile – carduh Dec 28 '15 at 23:12
  • @carduh A callback function will "remember" the _scope_ it was declared/defined. Hence yes, it has become a [passed] closure, when passed to a function. However, your code example doesn't contain any passed _callback_ function, that's why the question is not clear. @ c-smile These are definintely the shortest, most straightforward and understandable explanations of the subject I've ever seen. – Teemu Dec 28 '15 at 23:21
  • @Teemu the code is there to illustrate "the inverse is not true" – carduh Dec 28 '15 at 23:34
  • Ahh... If you mean if `myFunc` can access all the variables in `makeFunc`, then still yes, but only the variables declared in `makeFunc` and its outer scope. – Teemu Dec 28 '15 at 23:43