- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已阅读 another question在 stackoverflow 上,他说:
- A closure is one way of supporting first-class functions; it is an expression that can reference variables within its scope (when it was first declared), be assigned to a variable, be passed as an argument to a function, or be returned as a function result.
- Or, a closure is a stack frame which is allocated when a function starts its execution, and not freed after the function returns (as if a 'stack frame' were allocated on the heap rather than the stack!).
但我也读过MDN ,它说:
A closure is the combination of a function and the lexical environment within which that function was declared.
我认为它们完全不同。但是如果 MDN 是正确的,
var a=1;
function printA(){
console.log(a);
}
这段代码包含一个函数printA()和它的词法环境(变量a),是否意味着这段代码是闭包?还是 MDN 错了?
最佳答案
MDN 是正确的,闭包是函数及其对其外部环境的引用(允许它访问该环境的内容)的组合。另见 the Wikipedia entry . (笼统地说,闭包是一个函数,它具有对其外部环境的引用。)
您在 SO 上从其他地方引用的文字似乎略有偏差,但概念很难解释。
But if MDN is right,
var a=1;
function printA(){
console.log(a);
}this code include a function
printA()
and its lexical environment(variable a), does it mean this code is closure?
是的。事实上,JavaScript 中的所有 函数都是闭包(或者更准确地说,所有函数都与闭包相关联)。这就是全局变量的工作方式,因为所有函数都是全局词法环境的闭包。
作为实现优化,JavaScript 引擎有时可以优化函数与外部词法环境的链接,例如:
function double(a) {
return a * 2;
}
double
中没有任何内容是指在为调用 double
创建的词法环境中未定义的任何标识符,因此智能引擎可以(并且确实)优化掉链接完全。其他时候,他们可以确定只有部分外部环境被使用,因此只保留那些,允许其他人被垃圾收集。
关于javascript - MDN 上 JavaScript 闭包的定义是不是错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50529400/
此处,“OK”当然意味着 AYOR(风险由您自己承担),但如果避免与现有属性名称发生明显冲突,则不会出现可预见的问题。 Skyfield物体——尤其是行星——通常具有有限数量的属性。我经常编写简短的脚
我们有一个(假设)将在 1-2 年内完成的项目。到那时,JDK7(希望还有 Java7 JCP 规范)应该准备就绪。 但是,我想知道,甲骨文做出“愚蠢”决定的“危险”有多大,这会使 JDK7 成为一个
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
要知道,haswell是英特尔作为Ivy Bridge微架构的“第四代核心”继承者而开发的一种处理器微架构的代号。 1英特尔正式发布了基于这种微架构的CPU... More 但是,我想知道如何通过在
修改 setter 参数是否正常?假设我们有 setString 方法。我们真的想保留字符串的修剪形式。所以带有尾随空格的字符串是无效的,但我们不想抛出异常。 最好的解决办法是什么?修剪 setter
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。 Improve this
我写了一段代码,然后编译它没有错误。但是当我运行时,它会引发异常(java.lang.NoClassDefFoundError: TesKt$test$1$1)。 这是语言错误吗? private f
这道题是从算法导论的习题23.1-7演化而来的。 原问题是: 23.1-7争论如果一个图的所有边权重都是正的,那么连接所有顶点并且具有最小总权重的边的任何子集必须是一棵树。举例说明,如果我们允许某些权
假设我创建了一个 lambda 实例,稍后我想查询该对象以查看它是 proc 还是 lambda。如何做到这一点? .class() 方法不起作用。 irb(main):001:0> k = lamb
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
所以我正在将我的一个程序移植到一个新的游戏机上。问题是用于编译我的 c++ 应用程序的 SDK 不支持 __int16,但它支持 int16_t。使用 int16_t 代替 __int16 是否“安全
我最近偷看了 Url.IsLocalUrl() 方法的实现,该方法被广泛使用并被推荐为用于防止重定向攻击的措施之一。我惊讶地发现,如果我要为我的应用程序中的 Controller 的其中一个操作提供完
我读了这篇好文章 Angular onPush Change Detection Strategy 在某个时候他写道: It’s an anti-pattern to expose your subj
我是这样理解的: Innodb 中的缓冲池:它存储来自 Sql 表的索引和数据并缓存它以供将来查询,因此它不需要一直打扰 Mysql。 Memcached:用于存储从 Innodb 接收到的数据并对其
以防万一 vector 为空,(意外,异常(exception)情况) cppreference 在 vector::back() 上说: Calling back on an empty conta
我是一名优秀的程序员,十分优秀!