10

Just wondering, since closure is a function that has references to variables/methods outside it's definition. Every function closes over program's global variables (basically in every mainstream language, be it javascript/python/c/c+/whatever). So, consequently, every function is a closure?

Edit: let me reemphasize, I'm not talking only about closures in javascript but in a more general context

Novellizator
  • 13,633
  • 9
  • 43
  • 65
  • Possible duplicate of [How do JavaScript closures work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Nina Scholz Feb 01 '16 at 12:22
  • No, A function which returns a function is a closure. You can say data/variable hiding is good option in it. – Jai Feb 01 '16 at 12:23
  • 1
    @Jai That's just not true. JavaScript functions accept global functions and variables by closure (for example). – Madara's Ghost Feb 01 '16 at 12:28
  • @Jai: No, a closure is *not* "a function which returns a function." The function it returns is a closure, though. (Even ignoring the fact that all functions in JavaScript are closures.) – T.J. Crowder Feb 01 '16 at 12:28
  • possible duplicate of [Is it true that every function in JavaScript is a closure?](http://stackoverflow.com/q/30252621/1048572) – Bergi Feb 13 '16 at 12:42

2 Answers2

9

Yes, exactly. As you've identified, every function in JavaScript is a closure over at least one context: The global context. That's how/why global variables work in JavaScript.

We don't normally call them closures unless they close over some other context and actually make use of the fact that they do, but you're quite right that at a technical level, they all are.


Every function closes over program's global variables (basically in every mainstream language, be it javascript/c/c+/whatever).

I wouldn't generalize that far, no. Different languages have different ways of implementing global variables. Whether functions in those languages are all "closures" is probably open for debate, so I've restricted my answer above to JavaScript.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

closure is a function that has references to variables/methods outside its definition

No, this is a "function with free variables", not a "closure".

To quote wikipedia

...a closure is only distinct from a function with free variables when outside of the scope of the non-local variables, otherwise the defining environment and the execution environment coincide and there is nothing to distinguish these (static and dynamic binding can't be distinguished because the names resolve to the same values).

In other words, in some context, a closure is a reference to a function that binds variables from another context. Otherwise, it wouldn't make sense to call it a "closure".

georg
  • 211,518
  • 52
  • 313
  • 390
  • Now I get it, global context is a context of every function, hence function(even though implicitly) closed over global context cannot be a closure. – Novellizator Feb 01 '16 at 13:52
  • 1
    @Novellizator (and georg): This may or may not be true for other languages, but in JavaScript, all functions are closures. It's intrinsic to them, in JavaScript. There is nothing special about closures over *other* contexts that marks them out as distinct from functions that only close over the global context. See [§9.2.5 - *FunctionCreate*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-functioncreate) and its uses for details. – T.J. Crowder Feb 01 '16 at 14:35
  • @T.J.Crowder if every function is a closure, does that contradict to the wikipedia definition Georg mentioned? Could you then explain a difference between funciton with free variables and a closure? (not only in javascript, but some other language, such as python,...) – Novellizator Feb 01 '16 at 14:37
  • @Novellizator: I'm speaking only of JavaScript. They're all closures (in JS) because the mechanism by which global variables works in JavaScript is **exactly** the same mechanism by which closing over variables from other contexts works. There is no distinction, at all. This "functions with free variables" vs. "closures" semantic distinction may or may not be valid for languages I don't know as well as JavaScript, and as I said in my answer, we don't usually *call* them closures unless you're using them for their access to something non-global. But that doesn't mean they aren't closures. :-) – T.J. Crowder Feb 01 '16 at 14:44
  • 1
    You say no, but the definition you give seems to classify every function as a closure, simply a closure which cannot be used within lexical scopes. – kingfrito_5005 Nov 04 '16 at 20:36
  • 1
    "*A closure is only distinct from a function with free variables when outside of the scope of the non-local variables*" does not mean that a function is not a closure when inside of the scope of the non-local variables. It's still a closure, even if it is indistinguishable from a function with free variables in that context. – Bergi Feb 26 '22 at 20:41