What is closure?
--a closure is a function that can access interesting non-local variables
Variable Scope
When you declare a local variable, which variable has a scope. Generally local variables exist only within the block or function in which you declare them.
function() { var a = 1; console.log(a); // works} console.log(a); // fails
If I try to access a local variable, most languages would look for it in the current scope, then up through the parent SCOP Es until they reach the root scope.
var a = 1;function() { console.log(a); // works} console.log(a); // works
When a block or function is do with, its local variables was no longer needed and is usually blown out of memory.
This is what we normally expect things to work.
A closure is a persistent local variable scope
A closure is a persistent scope which holds in to local variables even after the code execution have moved out of this bloc K. Languages which support closure (such as JavaScript, Swift and Ruby) would allow you to keep a reference to a scope (inc Luding its parent scopes), even after the block in which those variables were declared have finished executing, provided yo U keep a reference to that block or function somewhere.
The scope object, and all its local variables, is tied to the function, and would persist as long as that function persist S.
This gives us function portability. We can expect any variables this were in scope when the function is first defined to still is in scope if we later call The function, even if we call the function in a completely different context.
For example
Here's a really simple example in JavaScript this illustrates the point:
outer = function() { var a = 1; var inner = function() { alert(a); } return inner; // this returns a function}var fnc = outer(); // execute outer to get inner fnc();
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 is in scope for the a inner function.
Normally when a function is exits, all its local variables is blown away. However, if we return the inner function and assign it to a variable fnc , so it persists after have outer exited, all of the variables, that were in scope, was inner defined also persist. The variable has a been closed over – it is within a closure.
Note that the variable are a totally private to fnc . This is a-a-creating private variables in a functional programming language such as JavaScript.
As you might being able to guess, when I call fnc() it alerts the value a of, which is "1".
In a language without closure, the variable a would has been garbage collected and thrown away when the function exited. Calling FNC would has thrown an error because a no longer exists.
In JavaScript, the variable a persists because variable scope was created when the function is first declared, and Pers Ists for as long as the function continues to exist.
abelongs to the scope of outer . The scope of have inner a parent pointer to the scope of outer . is fnc a variable which points to inner . a persists A s long as fnc persists. is a within the closure.
Go What is closure