问题标签 [lexical-closures]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
9265 浏览

javascript - javaScript中的词法作用域/闭包

我了解“js”中的函数具有词法范围(即,函数在定义时而不是在执行时创建它们的环境(范围)。)

当我只运行“f()”时,它会返回内部函数。我明白了,这就是'return'的作用!

为什么会出现“ReferenceError:b 未定义?” 但是上面的内部函数不能访问它的空间、f() 的空间等。因为'b' 被返回到全局空间,console.log() 不会工作吗?

但是,当我将 'f()' 分配给一个新变量并运行它时:

这将返回“b”,即“barb”,但是当您再次运行 console.log() 时,您将得到 'ReferenceError: 'b' is not defined';自从它被返回后,'b' 现在不是在全局范围内了吗?那么为什么 'x()' 不像 'f()' 一样返回内部函数呢?

0 投票
3 回答
808 浏览

lisp - Common Lisp 和 Scheme 词法闭包的区别

在 Common Lisp 中,我可以评估以下代码片段(在 SBCL 中),而不会收到任何语法错误的信号:

当我尝试评估相应的 Scheme 代码片段时(在 DrRacket 中):

它表示语法错误。

有人知道为什么这不能在 Scheme 中完成吗?

0 投票
1 回答
257 浏览

c# - “C#实现词法闭包”语句中的“词法”是什么意思?

我正在阅读有关 C# 和闭包的各种文章,Jon Skeet 的精彩“ C# in Depth ”,并且我看到诸如“C# 和 ruby​​ 实现词法闭包”之类的语句。

为什么作者包括“词汇”这个词?为什么不直接说他们“实施关闭”?

如果它只是实现了“闭包”,而不是“词法闭包(如果可能的话),那将意味着什么?

0 投票
2 回答
293 浏览

clojure - Clojure 关闭效率?

很多时候,我交换!使用匿名函数的原子值,该函数在计算新值时使用一个或多个外部值。有两种方法可以做到这一点,一种是我理解的闭包,另一种不是,我的问题是哪种方法更好/更有效?

这是一个简单的虚构示例 - 将变量数值添加到原子 - 显示两种方法:

这是一个虚构的示例,当然,您实际上不会编写此代码来解决此特定问题,因为:

做同样的事情而不需要任何额外的功能。

但在更复杂的情况下,您确实需要一个函数,然后问题就出现了。对我来说,从编码的角度来看,解决问题的两种方法的复杂性大致相同。如果是这样,我应该更喜欢哪个?我的工作假设是非封闭方法是更好的方法(因为编译器实现起来更简单)。

还有第三种解决问题的方法,即不使用匿名函数。如果您使用单独的命名函数,那么您不能使用闭包并且不会出现问题。但是内联匿名函数通常会使代码更具可读性,我想将这种模式留在我的工具包中。

谢谢!

编辑以回应 A. Webb 在下面的回答(这太长了,无法发表评论):

My use of the word "efficiency" in the question was misleading. Better words might have been "elegance" or "simplicity."

One of the things that I like about Clojure is that while you can write code to execute any particular algorithm faster in other languages, if you write idiomatic Clojure code it's going to be decently fast, and it's going to be simple, elegant, and maintainable. As the problems you're trying to solve get more complex, the simplicity, elegance and maintainability get more and more important. IMO, Clojure is the most "efficient" tool in this sense for solving a whole range of complex problems.

My question was really -- given that there are two ways that I can solve this problem, what's the more idiomatic and Clojure-esque way of doing it? For me when I ask that question, how 'fast' the two approaches are is one consideration. It's not the most important one, but I still think it's a legitimate consideration if this is a common pattern and the different approaches are a wash from other perspectives. I take A. Webb's answer below to be, "Whoa! Pull back from the weeds! The compiler will handle either approach just fine, and the relative efficiency of each approach is anyway unknowable without getting deeper into the weeds of target platforms and the like. So take your hint from the name of the language and when it makes sense to do so, use closures."

closing edit on April 10, 2014

I'm going to mark A. Webb's answer as accepted, although I'm really accepting A. Webb's answer and omiel's answer -- unfortunately I can't accept them both, and adding my own answer that rolls them up seems just a bit gratuitous.

One of the many things that I love about Clojure is the community of people who work together on it. Learning a computer language doesn't just mean learning code syntax -- more fundamentally it means learning patterns of thinking about and understanding problems. Clojure, and Lisp behind it, has an incredibly powerful set of such patterns. For example, homoiconicity ("code as data") means that you can dynamically generate code at compile time using macros, or destructuring allows you to concisely and readably unpack complex data structures. None of the patterns are unique to Clojure, but Clojure brings them all together in ways that make solving problems a joy. And the only way to learn those patterns is from people who know and use them already. When I first picked Clojure more than a year ago, one of the reasons that I picked it over Scala and other contenders was the reputation of the Clojure community for being helpful and constructive. And I haven't been disappointed -- this exchange around my question, like so many others on StackOverflow and elsewhere, shows how willing the community is to help a newcomer like me -- thank you!

0 投票
0 回答
178 浏览

javascript - PHP 闭包函数:为什么闭包必须是匿名函数?

lambda 或匿名函数只是一个没有名称的函数。

例如

闭包是一个函数,它可以访问未在其参数列表中指定的变量。在 PHP 5.3+ 中,这些变量在 use 关键字之后指定,并在执行闭包函数的函数定义时通过值或引用链接。

例如

在 JavaScript 中,所有函数都是闭包,因为当函数嵌套时,存在一个作用域链,而例如最里面的函数可以访问 1. 它的局部变量,2. 函数在其封闭范围内的局部变量,称为它的闭包作用域,3. 闭包作用域的闭包作用域的局部变量,称为闭包作用域的闭包作用域,...,以此类推,直到我们也可以访问 N. 定义在最高范围。在最内层范围内用 var 声明的变量会掩盖在外层范围内的变量,否则如果最内层变量与在外层范围内的变量同名但未用 var 声明,则使用来自外层范围的变量。

这等效于以下 PHP 代码:

所有这些都在这里描述

所以我的问题来了。为什么 PHP 中的闭包必须是 lambda 函数?在 JavaScript 中,每个函数都是一个闭包,但不一定是 lambda 函数。那么为什么我们不能在 PHP 中执行以下操作呢?

(致命错误:语法错误,意外的 T_USE,需要 ';' 或 '{')

0 投票
1 回答
59 浏览

javascript - 在 Javascript 中关闭的细微差别

我一直在尝试了解 Javascript 的功能组合技术,我想出了他的玩具准 MVC 工作代码来演示我的问题:

在这段代码中,我传入了一个绑定“钩子”变量并返回其作用域保留该绑定的函数的函数。这是我问题的症结所在。

我希望能够抽象出“钩子”绑定,所以我可以将“状态”绑定函数传递给一个函数,然后执行“钩子”绑定。我还没有弄清楚如何使用 Javascript 提供的后期绑定工具来做到这一点(应用/调用它)。我可以传递对象引用以保持状态并完成工作,但我对解决此问题的更多功能方法感兴趣。所以:

a)有没有办法做我正在尝试用Javascript中的基本闭包做的事情,如果是这样的话,或者

b)如果我应该使用基于“this”的后期绑定方法,那么最好的方法是什么?我假设这是在 Javascript 中泛化函数状态的行之有效的方法,但我为此编写的代码最终毫无用处。

0 投票
1 回答
228 浏览

emacs - 在 elisp 中围绕私有函数创建闭包

在 elisp 中,我有以下内容

当我运行繁荣功能时,我得到了错误

这里发生了什么?

我的意图是我想将私有/辅助函数和变量包装在一个闭包中,这样我就可以更好地组织我的代码,而不是一切都是全局的。

0 投票
1 回答
67 浏览

r - 并行化每个修改函数的父环境的独立函数调用

我想找到一种方法来并行化重复的独立函数调用,其中每个调用都会修改函数的父环境。函数的每次执行都是独立的,但是,由于各种原因,我无法考虑任何其他不依赖于修改函数父环境的实现。请参见下面的简化示例。有没有办法将父环境的副本传递给每个节点?我在linux系统上运行它。

0 投票
0 回答
71 浏览

javascript - javascript 分配哪些闭包变量?

我又一次深入研究了 javascript 内存泄漏。我的问题是在阅读Ilya Kantor的文章时提出的。

正如 Ilya 指出的那样,我知道 DOM-/COM-对象和 javascript 对象之间的循环引用导致 ie 的内存泄漏。

我也知道由于内部函数的词法环境被实现为david glasser解释的单例而导致的内存泄漏。

我的情况不同:哪些 javascript 引擎(如果有)会泄漏,data即分配datawrapped的词法环境,尽管在以下示例中没有引用?

  1. wrapped没有参考data
  2. 无论如何你都必须设置data=null不泄漏它吗?

伊利亚说(如果data设置为null

现在数据仍然作为 LexicalEnvironment 的属性保留在内存中,但它并没有占用太多空间。

大卫 说

幸运的是,JavaScript 实现(或至少是当前的 Chrome)足够聪明,可以注意到data[ wrapped...]

斯科特克里斯托弗在他的回答中提出了一个类似的问题,如果不是同一个问题。

这太模糊了,一个明确的答案会很棒!setInterval也许有人在使用上述方法时碰巧在 ie-vm 上绘制了内存使用图factory

0 投票
1 回答
743 浏览

bash - Closures get parent function name

Bash is “kind of function programming language” it dose not have classes. I managed to use encapsulation with Closures, but I want also to do some introspection to find also docker_ parent/super/base function (If you know add comments to define this correctly).

I managed this but with a dirty hack super=${FUNCNAME}. Is there any solution to use kind of PARENT_FUNCNAME? I have such file docker_.sh:

Got some results:

Solved

use

code:

Got some results: