Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <strong>closure</strong> is a pair of <strong>a function</strong> and <strong>the environment</strong> in which it was defined (assuming <a href="http://en.wikipedia.org/wiki/Scope_%28programming%29#Lexical_scoping" rel="noreferrer">lexical scoping</a>, which JavaScript uses). Thus, a closure's function can access variables in its environment; if no other function has access to that environment, then all of the variables in it are effectively private and only accessible through the closure's function.</p> <p>The example you provided demonstrates this reasonably well. I've added inline comments to explain the environments.</p> <pre><code>// Outside, we begin in the global environment. function greeter(name, age) { // When greeter is *invoked* and we're running the code here, a new // environment is created. Within this environment, the function's arguments // are bound to the variables `name' and `age'. // Within this environment, another new variable called `message' is created. var message = name + ", who is " + age + " years old, says hi!"; // Within the same environment (the one we're currently executing in), a // function is defined, which creates a new closure that references this // environment. Thus, this function can access the variables `message', `name', // and `age' within this environment, as well as all variables within any // parent environments (which is just the global environment in this example). return function greet() { console.log(message); }; } </code></pre> <p>When <code>var bobGreeter = greeter("Bob", 47);</code> is run, a new closure is created; that is, you've now got a new function instance along with the environment in which it was created. Therefore, your new function has a reference to the `message' variable within said environment, although no one else does.</p> <p>Extra reading: <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-21.html#%_sec_3.2" rel="noreferrer">SICP Ch 3.2</a>. Although it focuses on Scheme, the ideas are the same. If you understand this chapter well, you'll have a good foundation of how environments and lexical scoping work.</p> <p>Mozilla also has a page dedicated to <a href="https://developer.mozilla.org/en/JavaScript/Guide/Closures" rel="noreferrer">explaining closures</a>.</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