closure / 闭包

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
函数与对其状态即词法环境lexical environment)的引用共同构成闭包closure)。也就是说,闭包可以让你从内部函数访问外部函数作用域。在JavaScript,函数在每次创建时生成闭包。

Lexical scoping

词法作用域

Consider the following:
请看下面的代码:

1
2
3
4
5
6
7
8
9
10
11
function init() {
var name = "Mozilla"; // name is a local variable created by init //name 是由 init 创建的局部变量
function displayName() {
//displayName() is the inner function, a closure
// displayName() 是内部函数,即闭包
alert(name); // use variable declared in the parent function
// 父函数中声明的变量
}
displayName();
}
init();

init() creates a local variable called name and a function called displayName(). The displayName() function is an inner function that is defined inside init() and is only available within the body of the init() function. Note that the displayName() function has no local variables of its own. However, since inner functions have access to the variables of outer functions, displayName() can access the variable name declared in the parent function, init(). init() 创建了一个局部变量 name 和一个名为 displayName() 的函数。displayName() 是定义在 init() 里的内部函数,仅在该函数体内可被获取。请注意,displayName() 内没有自己的局部变量,然而它可以访问到外部函数的变量,所以 displayName() 可以使用父函数 init() 中声明的变量 name

Run the code and notice that the alert() statement within the displayName() function successfully displays the value of the name variable, which is declared in its parent function. This is an example of lexical scoping, which describes how a parser resolves variable names when functions are nested. The word “lexical” refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Nested functions have access to variables declared in their outer scope.
运行该代码后发现, displayName() 内的 alert() 语句显示出了 name 变量的值(该变量在父函数中声明)。这个词法作用域的例子描述了引擎是如何解析嵌套函数中的变量的。词法(lexical)一词表明,词法作用域根据声明变量的位置来确定该变量可被访问的位置。嵌套函数可获取声明于外部作用域的函数。

0%