Merge pull request #604 from jsonkao/patch-1

Small changes in Advanced Functions -> Closures
This commit is contained in:
Ilya Kantor 2018-11-09 10:25:30 +03:00 committed by GitHub
commit f4deba0356
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -339,7 +339,7 @@ Here's what's going on in the `makeCounter` example step-by-step, follow it to m
![](lexenv-nested-makecounter-3.png)
Please note that on this step the inner function was created, but not yet called. The code inside `function() { return count++; }` is not running, we're going to return it.
Please note that on this step the inner function was created, but not yet called. The code inside `function() { return count++; }` is not running; we're going to return it soon.
4. As the execution goes on, the call to `makeCounter()` finishes, and the result (the tiny nested function) is assigned to the global variable `counter`:
@ -380,7 +380,7 @@ But if there were no `let name` in `makeWorker()`, then the search would go outs
```smart header="Closures"
There is a general programming term "closure", that developers generally should know.
A [closure](https://en.wikipedia.org/wiki/Closure_(computer_programming)) is a function that remembers its outer variables and can access them. In some languages, that's not possible, or a function should be written in a special way to make it happen. But as explained above, in JavaScript all functions are naturally closures (there is only one exclusion, to be covered in <info:new-function>).
A [closure](https://en.wikipedia.org/wiki/Closure_(computer_programming)) is a function that remembers its outer variables and can access them. In some languages, that's not possible, or a function should be written in a special way to make it happen. But as explained above, in JavaScript, all functions are naturally closures (there is only one exclusion, to be covered in <info:new-function>).
That is: they automatically remember where they were created using a hidden `[[Environment]]` property, and all of them can access outer variables.