-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JavaScript scope and closure documentation
- Loading branch information
1 parent
e10a5a4
commit b7320ae
Showing
3 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
|
||
sidebar_position: 1 | ||
tags: | ||
|
||
- scope | ||
|
||
--- | ||
|
||
# Scope | ||
|
||
Where and how to look for things. JS have two lexical scopes global and function | ||
|
||
變數在程式中可以被存取的範圍,可分為區域變數,全域變數。 | ||
|
||
![alt text](image-3.png) | ||
|
||
# Closure | ||
|
||
Closure is when a function “remembers” its lexical scope even when the function is executed outside that lexical scope. The combination of the function and it’s environment is known as a closure. | ||
|
||
閉包是個捕捉了外部函式變數(或使之繼續存活)的函式,它包含了一個函式,以及函式被建立時所在的環境 | ||
|
||
![alt text](image-4.png) | ||
|
||
基本上函式離開執行環境時,也會同時將佔用的記憶體空間給釋放出來。例如以上 x變數應該在執行完畢就會在 memory (記憶體) 中被清掉。但因為 closure 特性此 x變數還會繼續被保留 |