Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Variable scope</h2> <p>When you declare a local variable, that variable has a scope. Generally local variables exist only within the block or function in which you declare them. </p> <pre><code>function() { var a = 1; console.log(a); // works } console.log(a); // fails </code></pre> <p>If I try to access a local variable, most languages will look for it in the current scope, then up through the parent scopes until they reach the root scope.</p> <pre><code>var a = 1; function() { console.log(a); // works } console.log(a); // works </code></pre> <p>When a block or function is done with, its local variables are no longer needed and are usually blown out of memory.</p> <p>This is how we normally expect things to work.</p> <h2>A closure is a persistent local variable scope</h2> <p>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.</p> <p>The scope object, and all its local variables, are tied to the function, and will persist as long as that function persists.</p> <p>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.</p> <h2>For example</h2> <p>Here's a really simple example in JavaScript that illustrates the point:</p> <pre><code>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(); </code></pre> <p>Here I have defined a function within a function. The inner function gains access to all the outer function's local variables, including <code>a</code>. The variable <code>a</code> is in scope for the inner function.</p> <p>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 <code>fnc</code>, so that it persists after <code>outer</code> has exited, <strong>all of the variables that were in scope when <code>inner</code> was defined also persist</strong>. The variable <code>a</code> has been closed over -- it is within a closure.</p> <p>Note that the variable <code>a</code> is totally private to <code>fnc</code>. This is a way of creating private variables in a functional programming language such as JavaScript.</p> <p>As you might be able to guess, when I call <code>fnc()</code> it prints the value of <code>a</code>, which is "1". </p> <p>In a language without closure, the variable <code>a</code> would have been garbage collected and thrown away when the function <code>outer</code> exited. Calling fnc would have thrown an error because <code>a</code> no longer exists.</p> <p>In JavaScript, the variable <code>a</code> persists because variable scope is created when the function is first declared, and persists for as long as the function continues to exist.</p> <p><code>a</code> belongs to the scope of <code>outer</code>. The scope of <code>inner</code> has a parent pointer to the scope of <code>outer</code>. <code>fnc</code> is a variable which points to <code>inner</code>. <code>a</code> persists as long as <code>fnc</code> persists. <code>a</code> is within the closure.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload