1

I've read about closures many times here.

But I'm really confusing to know exactly what do closure mean?

  • Is variable a closure ?
  • Or, is function is a closure ?
  • Or, both can be called a closure ?
Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • @MikeW yes, exactly, but I couldn't understand exactly from there, can you please tell me in short what the closure is? a variable or a function or both ? – Bhojendra Rauniyar Sep 27 '14 at 23:12
  • The topmost answer on the linked question gives you an example of a closure. If that's not clear I don't see what I can add. –  Sep 27 '14 at 23:16
  • yes, from accepted answer, I'm confusing a lot because variable is also called a closure, function also called a closure, but in some site function is to be said as a closure, but exactly what the closure is ? – Bhojendra Rauniyar Sep 27 '14 at 23:19
  • 1
    https://en.wikipedia.org/wiki/Closure_(computer_programming) – Quentin Sep 27 '14 at 23:21
  • umm, variable is called to be a closure ?? – Bhojendra Rauniyar Sep 27 '14 at 23:25
  • *Is variable a closure* is like asking, is the number 2 blue? – Derek 朕會功夫 Sep 27 '14 at 23:48
  • For example, let's say you have a function `function inc(){ var i = 0; return function(){ return i++; };}`, if you do `var getI = inc()`, then you have created a closure. You can keep calling `get()` again and again to get an increased value each time. – Derek 朕會功夫 Sep 27 '14 at 23:54
  • The easiest, most natural explanation for closures is that a closure is a private global variable. Whereas all functions/methods in your program can see all global variables, only the functions that share a closure can see the enclosed variable. Because of this, it is most natural way to think what a closure is. But the word "closure" actually means the mechanism that allows this behavior to happen. Not the variable nor the behavior of the variable to act as a private global variable. – slebetman Sep 28 '14 at 01:39
  • So technical definition of a closure is: a construct (in some languages it's inner functions, in those cases it's called lexical closures, in some languages it's a keyword, in others it's a syntax like `for` or `if` or `struct`) that allows variables in the defining scope to be enclosed by the defined scope. – slebetman Sep 28 '14 at 01:42
  • But because the technical definition is so "meta" it's often easier to talk about closures in the concrete form: variables, functions etc. – slebetman Sep 28 '14 at 01:43
  • BTW, it is exactly like asking is the number 2 blue. In that the answer to the question "is the number 2 blue" is yes. The number 2 in hexadecimal is 000002 which is a very dark blue (remember, it's rrggbb). Here, color is the "meta" concept and RGB colors is the concrete representation of color. The same as closure is the meta concept while the variable enclosed is concrete. So depending on the code you're looking at, asking "is that variable a closure" can either be yes or no. – slebetman Sep 28 '14 at 01:49

1 Answers1

2

A closure is a container for variables, it lets a function access variables from the scope where it was created even when it is called in a different scope.

Example:

function a() {
  // a local variable
  var x = 42;
  function f() {
    // use the local variable
    return x;
  }
  // return the function f
  return f;
}

// call a to get the inner function
var fx = a();
// call the inner function from out here
var y = fx();
// now y contains the 42 that was grabbed from the variable x inside the function

When the function f is created, it has a closure with it that contains the variable x from the scope where it was created. When the function is called from the global scope it can still access the variable x from the local scope as it is in the closure.

You could simulate the function of the closure by putting variables in an object, and send that along with the function, so that the function can use it later:

function a() {
  // a "closure"
  var o = { x: 42 };
  function f(closure) {
    // use the "closure"
    return closure.x;
  }
  // return the "closure" and the function f
  return { f: f, o: o };
}

// call a to get the inner function and the "closure"
var fx = a();
// call the inner function with the "closure"
var y = fx.f(fx.o);
// now y contains the 42 that was grabbed from the variable x in the "closure"

Note: The closure contains any identifer in the scope, so in the first example the closure for the function f also contains the identifier f which is the function. The function can call itself using the name f from anywhere, although the identifier f is local to the function a.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005