diff --git a/1-js/06-advanced-functions/03-closure/article.md b/1-js/06-advanced-functions/03-closure/article.md index 465ba311..0c7dcb9e 100644 --- a/1-js/06-advanced-functions/03-closure/article.md +++ b/1-js/06-advanced-functions/03-closure/article.md @@ -1,9 +1,9 @@ # Closure -JavaScript is a very function-oriented language. It gives us a lot of freedom. A function can be created at one moment, then copied to another variable or passed as an argument to another function and called from a totally different place later. +JavaScript is a very function-oriented language. It gives us a lot of freedom. A function can be created dynamically, copied to another variable or passed as an argument to another function and called from a totally different place later. -We know that a function can access variables outside of it; this feature is used quite often. +We know that a function can access variables outside of it, this feature is used quite often. But what happens when an outer variable changes? Does a function get the most recent value or the one that existed when the function was created? @@ -63,24 +63,24 @@ Let's consider two situations to begin with, and then study the internal mechani To understand what's going on, let's first discuss what a "variable" actually is. -In JavaScript, every running function, code block, and the script as a whole have an associated object known as the *Lexical Environment*. +In JavaScript, every running function, code block `{...}`, and the script as a whole have an internal (hidden) associated object known as the *Lexical Environment*. The Lexical Environment object consists of two parts: -1. *Environment Record* -- an object that has all local variables as its properties (and some other information like the value of `this`). -2. A reference to the *outer lexical environment*, usually the one associated with the code lexically right outside of it (outside of the current curly brackets). +1. *Environment Record* -- an object that stores all local variables as its properties (and some other information like the value of `this`). +2. A reference to the *outer lexical environment*, the one associated with the outer code. -**So, a "variable" is just a property of the special internal object, Environment Record. "To get or change a variable" means "to get or change a property of that object".** +**So, a "variable" is just a property of the special internal object, `Environment Record`. "To get or change a variable" means "to get or change a property of that object".** For instance, in this simple code, there is only one Lexical Environment: ![lexical environment](lexical-environment-global.png) -This is a so-called global Lexical Environment, associated with the whole script. For browsers, all `