Closure (nonfiction): Difference between revisions

From Gnomon Chronicles
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 11: Line 11:
You create a closure by adding a function inside another function.
You create a closure by adding a function inside another function.


<code>
<source lang="javascript">
function showName (firstName, lastName)
function showName (firstName, lastName)
var nameIntro = "Your name is ";
var nameIntro = "Your name is ";
// this inner function has access to the outer function's variables, including the parameter
// this inner function has access to the outer function's variables, including the parameter
function makeFullName ()  
function makeFullName ()  
return nameIntro + firstName + " " + lastName;
  return nameIntro + firstName + " " + lastName;
}
return makeFullName ();
}
}
return makeFullName ();
 
showName ("Michael", "Jackson"); // Your name is Michael Jackson

</source>
 
Rules and side effects:
 
* Closures have access to the outer function’s variable even after the outer function returns.
* Closures store references to the outer function’s variables; they do not store the actual value.
* Because closures have access to the updated values of the outer function’s variables, they can also lead to bugs when the outer function’s variable changes.
 
Source: http://javascriptissexy.com/understand-javascript-closures-with-ease/
 
== Community discussion ==
 
=== Stack Exchange: What is a Closure ===
 
* https://softwareengineering.stackexchange.com/questions/40454/what-is-a-closure
 
=== Stack Overflow: Closure Because of What it Can Do or Because it Does ===
 
* https://stackoverflow.com/questions/4103750/closure-because-of-what-it-can-do-or-because-it-does/4103834
 
=== Stack Overflow: What is a Closure? ===
 
==== Example ====
 
A closure is a persistent scope which holds on to local variables even after the code execution has moved out of that block. Languages which support closure (such as JavaScript, Swift, and Ruby) will allow you to keep a reference to a scope (including its parent scopes), even after the block in which those variables were declared has finished executing, provided you keep a reference to that block or function somewhere.
 
The scope object and all its local variables are tied to the function and will persist as long as that function persists.
 
This gives us function portability. We can expect any variables that were in scope when the function was first defined to still be in scope when we later call the function, even if we call the function in a completely different context.
 
Here's a really simple example in JavaScript that illustrates the point:
 
<source lang="javascript">
outer = function() {
  var a = 1;
  var inner = function() {
    console.log(a);
  }
  return inner; // this returns a function
}
}
showName ("Michael", "Jackson"); // Your name is Michael Jackson

</code>


http://javascriptissexy.com/understand-javascript-closures-with-ease/
var fnc = outer(); // execute outer to get inner
fnc();
</source>
 
Here I have defined a function within a function. The inner function gains access to all the outer function's local variables, including a. The variable a is in scope for the inner function.
 
Normally when a function exits, all its local variables are blown away. However, if we return the inner function and assign it to a variable fnc so that it persists after outer has exited, all of the variables that were in scope when inner was defined also persist. The variable a has been closed over -- it is within a closure.
 
Note that the variable a is totally private to fnc. This is a way of creating private variables in a functional programming language such as JavaScript.
 
As you might be able to guess, when I call fnc() it prints the value of a, which is "1".
 
In a language without closure, the variable a would have been garbage collected and thrown away when the function outer exited. Calling fnc would have thrown an error because a no longer exists.
 
In JavaScript, the variable a persists because the variable scope is created when the function is first declared and persists for as long as the function continues to exist.
 
a belongs to the scope of outer. The scope of inner has a parent pointer to the scope of outer. fnc is a variable which points to inner. a persists as long as fnc persists. a is within the closure.
 
==== Example ====
 
...
 
* https://stackoverflow.com/questions/36636/what-is-a-closure
 
== In the News ==
 
<gallery>
</gallery>
 
== Fiction cross-reference ==
 
* [[Gnomon algorithm]]
* [[Gnomon Chronicles]]
 
== Nonfiction cross-reference ==
 
 
[[Category:Nonfiction (nonfiction)]]
[[Category:Computer science (nonfiction)]]

Latest revision as of 21:20, 10 January 2019

In programming languages, a closure (also lexical closure or function closure) is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function[a] together with an environment. The environment is a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created. Unlike a plain function, a closure allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

https://en.wikipedia.org/wiki/Closure_(computer_programming)

What is a closure?

A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters. Note that the inner function cannot call the outer function’s arguments object, however, even though it can call the outer function’s parameters directly.

You create a closure by adding a function inside another function.

<source lang="javascript"> function showName (firstName, lastName)

var nameIntro = "Your name is ";
// this inner function has access to the outer function's variables, including the parameter
function makeFullName () 
 return nameIntro + firstName + " " + lastName;
}
return makeFullName ();

}

showName ("Michael", "Jackson"); // Your name is Michael Jackson
 </source>

Rules and side effects:

  • Closures have access to the outer function’s variable even after the outer function returns.
  • Closures store references to the outer function’s variables; they do not store the actual value.
  • Because closures have access to the updated values of the outer function’s variables, they can also lead to bugs when the outer function’s variable changes.

Source: http://javascriptissexy.com/understand-javascript-closures-with-ease/

Community discussion

Stack Exchange: What is a Closure

Stack Overflow: Closure Because of What it Can Do or Because it Does

Stack Overflow: What is a Closure?

Example

A closure is a persistent scope which holds on to local variables even after the code execution has moved out of that block. Languages which support closure (such as JavaScript, Swift, and Ruby) will allow you to keep a reference to a scope (including its parent scopes), even after the block in which those variables were declared has finished executing, provided you keep a reference to that block or function somewhere.

The scope object and all its local variables are tied to the function and will persist as long as that function persists.

This gives us function portability. We can expect any variables that were in scope when the function was first defined to still be in scope when we later call the function, even if we call the function in a completely different context.

Here's a really simple example in JavaScript that illustrates the point:

<source lang="javascript"> outer = function() {

 var a = 1;
 var inner = function() {
   console.log(a);
 }
 return inner; // this returns a function

}

var fnc = outer(); // execute outer to get inner fnc(); </source>

Here I have defined a function within a function. The inner function gains access to all the outer function's local variables, including a. The variable a is in scope for the inner function.

Normally when a function exits, all its local variables are blown away. However, if we return the inner function and assign it to a variable fnc so that it persists after outer has exited, all of the variables that were in scope when inner was defined also persist. The variable a has been closed over -- it is within a closure.

Note that the variable a is totally private to fnc. This is a way of creating private variables in a functional programming language such as JavaScript.

As you might be able to guess, when I call fnc() it prints the value of a, which is "1".

In a language without closure, the variable a would have been garbage collected and thrown away when the function outer exited. Calling fnc would have thrown an error because a no longer exists.

In JavaScript, the variable a persists because the variable scope is created when the function is first declared and persists for as long as the function continues to exist.

a belongs to the scope of outer. The scope of inner has a parent pointer to the scope of outer. fnc is a variable which points to inner. a persists as long as fnc persists. a is within the closure.

Example

...

In the News

Fiction cross-reference

Nonfiction cross-reference