Small changes in Advanced Functions -> Closures
This commit is contained in:
parent
69bd4b5f3c
commit
7b0be24ffd
1 changed files with 2 additions and 2 deletions
|
@ -339,7 +339,7 @@ Here's what's going on in the `makeCounter` example step-by-step, follow it to m
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
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`:
|
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"
|
```smart header="Closures"
|
||||||
There is a general programming term "closure", that developers generally should know.
|
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.
|
That is: they automatically remember where they were created using a hidden `[[Environment]]` property, and all of them can access outer variables.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue