幫你精通JavaScript:什么是閉包?
一 、詞法定義域 Lexical
Closure閉包是編程語言Lexical Scoping的專有屬性,區(qū)別于dynamic scoping。即函數(shù)執(zhí)行調(diào)用的是其在定義過程中的”變量定義域“,而非其在調(diào)用時候的變量定義域。
Javascript的函數(shù)的初始狀態(tài)不僅包括函數(shù)本體而且包括函數(shù)定義過程所在的定義域。
- Like most modern programming languages, JavaScript uses lexical scoping. This means that functions are executed using the variable scope that was in effect when they were defined, not the variable scope that is in effect when they are invoked. In order to implement lexical scoping, the internal state of a JavaScript function object must include not only the code of the function but also a reference to the scope in which the function definition appears. This combination of a function object and a scope (a set of variable bindings) in which the function’s variables are resolved is called a closure in the computer science literature.
看下面的例子:
- function makeCounter () {
- let counter = 0;
- return function() {return counter++;};
- }
- let counter = makeCounter();
- console.log(counter());
- console.log(counter());
- console.log(counter());
- #+RESULTS:
- : 0
- : 1
- : 2
對這個嵌套函數(shù)而言,最有意思的一點(diǎn)是:當(dāng)外部函數(shù)被調(diào)用返回后(這里是makeCounter()), 再也沒有任何手段能夠觸及到 counter 這個變量。只有內(nèi)嵌函數(shù)擁有專屬權(quán)限抵達(dá)該變量。
二、Closure的標(biāo)準(zhǔn)定義
開發(fā)者通常應(yīng)該都知道“閉包”這個通用的編程術(shù)語。
閉包 是指內(nèi)部函數(shù)總是可以訪問其所在的外部函數(shù)中聲明的變量和參數(shù),即使在其外部函數(shù)被返回(壽命終結(jié))了之后。在某些編程語言中,這是不可能的,或者應(yīng)該以特殊的方式編寫函數(shù)來實現(xiàn)。但是如上所述,在 JavaScript 中,所有函數(shù)都是天生閉包的(只有一個例外,將在 "new Function" 語法 中講到)。
也就是說:JavaScript 中的函數(shù)會自動通過隱藏的 [[Environment]] 屬性記住創(chuàng)建它們的位置,所以它們都可以訪問外部變量。
在面試時,前端開發(fā)者通常會被問到“什么是閉包?”,正確的回答應(yīng)該是閉包的定義,并解釋清楚為什么 JavaScript 中的所有函數(shù)都是閉包的,以及可能的關(guān)于 [[Environment]] 屬性和詞法環(huán)境原理的技術(shù)細(xì)節(jié)。