您的位置:首页 > 其它

Closures 闭包

2016-05-11 18:52 197 查看
        在编程语言中,闭包(词法闭包or函数闭包)是一种在支持first-class functions语言中,实现词法作用域的名字绑定的技术。操作实现上,闭包指将函数与其环境一起保存起来的记录:当闭包被创建时,映射关联每个函数的非局部(本地)变量 到 它的值或者名字已被限制的存储位置。闭包不同于普通的函数,允许函数访问这些通过闭包引用而捕获的变量,甚至在变量作用域之外调用函数。 

        例子: 以下程序片断定义了高阶函数 startAt ,它包括x参数和一个嵌套的函数incrementBy。嵌套的函数访问了x,因为incrementBy在x的词义作用域中,即使x不在incrementBy本地。函数
startAt返回了容纳了函数incrementBy的闭包,incrementBy将y的值与x的值相加,也从startAt的这次调用返回了x变量的引用,于是incrementBy一旦被调用就知道去哪里找到x:

function startAt(x)
function incrementBy(y)
return x + y
return incrementBy

variable closure1 = startAt(1)
variable closure2 = startAt(5)
       注意,因为startAt返回的是函数,变量closure1和closure2是函数类型的。closure1(3)调用将返回4,而closure2(3)将返回8。因为closure1和closure2引用同一个函数incrementBy,关联的环境不同。所以在两次调用中,调用的闭包绑定名字x到值不同的不同变量。最终函数得到不同的结果。

In programming
languages, closures (also lexical
closures
 or function
closures
) are a technique for implementing lexically
scoped name
binding in languages with first-class
functions. Operationally,
a closure is a record storing
function together
with an environment: a mapping associating
each free
variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or storage
location to which the name was bound when the closure was created. A
closure—unlike a plain function—allows the function to access those captured variables through
the closure's reference to them, even when the function is invoked outside their scope.

Example. The following program fragment defines a higher-order
function startAt with a parameter x and a nested
function incrementBy. The nested function incrementBy has access to x, because incrementBy is
in the lexical scope of x, even though x is not local to incrementBy. The function startAt returns
a closure containing the function incrementBy, which adds the y value to the x value, and a reference to the variable x from
this invocation of startAt, so incrementBy will know where to find it once invoked:

function startAt(x)
function incrementBy(y)
return x + y
return incrementBy

variable closure1 = startAt(1)
variable closure2 = startAt(5)

Note that, as startAt returns
a function, the variables closure1 and closure2 are
of function type.
Invoking closure1(3) will
return 4, while invoking closure2(3) will
return 8. While closure1 and closure2 refer
to the same function incrementBy,
the associated environments differ, and invoking the closures will bind the name x to
two distinct variables with different values in the two invocations, thus evaluating the function to different results.

原文链接: https://en.wikipedia.org/wiki/Closure_(computer_programming)
以下内容解释了:first-class functions和higher-order
function

 1.     In computer
science, a programming
language is said to have first-class
functions if it treats functions as first-class
citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other
functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous
functions (function literals) as well. In
languages with first-class functions, thenames of
functions do not have any special status; they are treated like ordinary variables with
function type. The
term was coined by Christopher
Strachey in the context of "functions as first-class citizens" in the mid-1960s.

2.  A function type depends on the type of
the parameters and the result type of the function。

In mathematics and computer
science, a higher-order function (also functionalfunctional
form
 or functor)
is a function that
does at least one of the following:

takes one or more functions as arguments 
returns a function as its result.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: