Closure is when a "function" "remembers" its lexical scope even when the function is executed outside that lexical scope.
Closure 是指說一個function記住了他在被定義的當下時的lexical scope,即使function在執行的時候是在當時的lexical scope之外
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
functionfoo() {
var bar = "bar";
functionbaz() {
console.log(bar); // reference bar
}
bam(baz); // 透過bam 傳出去
}
functionbam(baz) {
baz(); // 在不同的環境下執行
}
foo();
function baz 裡面的bar 由於reference了 foo function中的bar
Closure is when a "function" "remembers" its lexical scope even when the function is executed outside that lexical scope.
Closure 是指說一個function記住了他在被定義的當下時的lexical scope,即使function在執行的時候是在當時的lexical scope之外