JavaScript Closures

本文为翻译,原文地址请戳 JavaScript Closures

介绍

Closure
A “closure” is an expression (typically a function) that can have free variables together with an environment that binds those variables (that “closes” the expression).

闭包是一个“闭合”的表达式(通常是一个函数),处于同一封闭作用域下的变量之间可以自由访问。

闭包是ECMAScript最强大的特性之一,不理解就不能最大化利用特性价值。然而,创建一个闭包很容易(有时候是个意外),同时闭包的创建具有潜在的不良影响,尤其是在相对常见的web浏览器环境。为了避免遭遇这些弊端,同时尽可能的利用它的优势,我们有必要去理解它的机制。这些很依赖标识符解析时,作用域链(scope chains)所扮演的角色,以及对对象属性名的解析。

The simple explanation of a Closure is that ECMAScript allows inner functions; function definitions and function expressions that are inside the function bodes of other functions. And that those inner functions are allowed access to all of the local variables, parameters and declared inner functions within their outer function(s). A closure is formed when one of those inner functions is made accessible outside of the function in which it was contained, so that it may be executed after the outer function has returned. At which point it still has access to the local variables, parameters and inner function declarations of its outer function. Those local variables, parameter and function declarations (initially) have the values that they had when the outer function returned and may be interacted with by the inner function.

简单解释闭包就是ECMAScript允许的内部功能;

未完待续。。。。。。