0

I know how to use "closures"... but what does the word closure actually refer to?

On MDN the definition is that it is a function. Here is the page about closures. The first sentence is, "Closures are functions that refer to independent (free) variables." and in the first code example there is a comment highlighting the closure function. However, the second sentence seems to suggest that the closure is really the persistent scope that the inner function resides in. That's what this other stack overflow answer suggest, too (search word "persistent").

So, what is it? The function or the persistent scope?

Community
  • 1
  • 1
noob-in-need
  • 861
  • 1
  • 7
  • 13
  • 1
    couldn't it be both? – John Ruddell Oct 07 '15 at 02:35
  • These two kind of go hand-in-hand. Some people use the term interchangeable. The important thing is to understand how the combination works. – Thilo Oct 07 '15 at 02:36
  • @JohnRuddell how could it be? – noob-in-need Oct 07 '15 at 02:37
  • Closures are not functions, it's a process that affects functions. would read better changing "Closures are functions" to "Closures are created by functions" – dandavis Oct 07 '15 at 02:38
  • 1
    @dandavis so is that mozilla page wrong? – noob-in-need Oct 07 '15 at 02:38
  • something can be a Constructor and a Function at the same time, why not call it a closure if that's what it does? They aren't mutually exclusive, but not all functions have/are closure(s). the same page says "A closure is a special kind of object that combines two things: a function, and the environment in which that function was created" – dandavis Oct 07 '15 at 02:40
  • @dandavis so then would you say a function's scope and a closure are the same thing? – noob-in-need Oct 07 '15 at 02:43
  • they are closely related, and indeed without using globals, closure'd lexical names make up the majority of the scope of most plain functions you find in code. – dandavis Oct 07 '15 at 02:44
  • 3
    The scope persistence wouldn't exist without the function, so does it really matter which is the closure? Would it make you a better programmer to know, or would it merely give you some closure? ;) – Steven Moseley Oct 07 '15 at 02:47

2 Answers2

3

Technically, a closure is the mechanism behind how persistent scope works in functions - in other words, it's the implementation. That's what it originally meant in terms of Lisp. And in some cases that's still what it means - just look at the explanations of what closures are in various Lisp dialects and they almost all try to explain it in terms of how the compiler/interpreter implements closures. Because that was how people used to explain scope.

The first time I came across a much simpler explanation of closures (that is, explaining the behavior instead of the mechanics) was in javascript. Now that more people are used to the idea of closures, the word itself has grown to mean:

  • the captured scope of an inner function allowing the function to refer to the variables of the outer function (this is the closest to the original meaning)

  • the inner function itself (typically in languages that call first-class functions or lambdas "closure")

  • the free variable captured by the closure

I personally prefer the last meaning because it captures the essence of what closures are: a form of variable sharing, kind of like globals. However, if you want to be pedantic, only the first meaning is actually the technical meaning: "closure" refers to the captured scope.

slebetman
  • 109,858
  • 19
  • 140
  • 171
0

To answer your question as to whether the closure is the function or the environment, it's both - it's the combination of the function and the environment which it makes use of. It's especially important (and may really only exist) in the context of languages where functions can be defined (often anonymously) and passed around as parameters to other functions (witness, for example, the use of blocks in Smalltalk, anonymous functions in the Lisp/Scheme sphere, and etc). The issue is this: if global function A is created during the execution of function B and A references a parameter named P which was passed to B; and if A is subsequently stored in a variable V, and is then invoked through V after B has returned, A still needs to have access to P (which was a parameter to B, which has long since finished by the time A is executed) in order to do its work; thus, A must have a "closure over P" in order to do its job successfully. The "closure" is a combination of function A itself and the environment set up when A is compiled which stores a copy or reference or what-have-you of P so that A can do its job successfully.

Note that despite having used languages which make extensive use of closures (particularly Smalltalk and some Lisp variants) this is usually so well buried that it's beyond transparent - you don't even notice it. If you notice that there's a closure, IMO it's not well done.

YMMV.

Best of luck.