什么是“闭合”参考在JavaScript? [英] What exactly does "closure" refer to in JavaScript?

查看:82
本文介绍了什么是“闭合”参考在JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白什么是闭包,但我有一些麻烦,正确的是闭包指的是什么。我已经看到了许多网站中使用的术语,但很少会同意它的实际定义。





  • 是否是外部函数的作用域?

  • 是否是内部(返回)函数的范围?

  • 是否可以将这些变量保存在堆栈上的概念返回函数之后的框架?



有人可以告诉我什么 closure 是指 rel =nofollow noreferrer> JavaScript Closures



两个一句话摘要:


闭包是
函数的局部变量 - 在
函数返回后保持活动,或



闭包是一个堆栈框架,
在函数
返回时不会释放。 (如果一个'stack-frame'是
malloc'ed,而不是在
堆栈!)


关于关闭的非常好的文章



JavaScript Closures


closure是一个表达式
(通常是一个函数)
自由变量和一个
环境绑定这些变量
(关闭表达式)。



简单的解释a Closure是
,ECMAScript允许内部
函数;函数定义和
里面的函数表达式
其他函数的函数bodes。
这些内部函数是
允许访问所有的本地
变量,参数和声明
内部函数在它们的外部
函数内。当
其中一个内部函数的
访问它包含的
中的函数之外时,形成闭包,因此它可以
在外部函数
已返回。此时它仍然
可以访问局部变量,
参数和内部函数
其外部函数的声明。
这些局部变量,参数和
函数声明(最初)有
的值,当
外部函数返回时,它们可能是
与内部


这是一个很好的例子



JavaScript,时间到grok闭包


I understand what closures are, but I am having some trouble grokking exactly what the term closure refers to. I have seen the term used in many websites, but rarely do they agree on the actual definition of it.

  • Is it the variables that are kept on the stack frame?
  • Is it the function that is being returned?
  • Is it the scope of the outer function?
  • Is it the scope of the inner (returned) function?
  • Is it maybe the concept of keeping the variables on the stack-frame after returning the function?

Can someone tell me exactly to what closure refers to?

解决方案

From JavaScript Closures

Two one-sentence summaries:

A closure is the local variables for a function - kept alive after the function has returned, or

A closure is a stack-frame which is not deallocated when the function returns. (as if a 'stack-frame' were malloc'ed instead of being on the stack!)

A very good article on closures

Javascript Closures

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).

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.

A good example over here

JavaScript, time to grok closures

这篇关于什么是“闭合”参考在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆