每个函数都是闭包吗? [英] Is every function a closure?

查看:227
本文介绍了每个函数都是闭包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道,因为闭包是一个函数,它有定义之外的变量/方法的引用。
每个函数关闭程序的全局变量(基本上在每个主流语言中,无论是javascript / python / c / c + / whatever)。
因此,每个函数都是一个闭包?

Just wondering, since closure is a function that has references to variables/methods outside it's definition. Every function closes over program's global variables (basically in every mainstream language, be it javascript/python/c/c+/whatever). So, consequently, every function is a closure?

编辑:让我重新强调,我不只是谈论javascript中的闭包,上下文

let me reemphasize, I'm not talking only about closures in javascript but in a more general context

推荐答案


closure是一个函数, >

closure is a function that has references to variables/methods outside its definition

不,这是一个带有自由变量的函数,而不是一个闭包。

No, this is a "function with free variables", not a "closure".

引用维基百科


...当非局部变量的范围之外时,闭包仅仅与具有自由变量的函数不同,否则定义环境和执行环境重合,没有什么可以区分这些(静态和动态绑定不能区分,因为名称解析为相同的值)。

...a closure is only distinct from a function with free variables when outside of the scope of the non-local variables, otherwise the defining environment and the execution environment coincide and there is nothing to distinguish these (static and dynamic binding can't be distinguished because the names resolve to the same values).

换句话说,在某些上下文中,闭包是对从另一个上下文绑定变量的函数的引用。否则,将其称为关闭是没有意义的。

In other words, in some context, a closure is a reference to a function that binds variables from another context. Otherwise, it wouldn't make sense to call it a "closure".

这篇关于每个函数都是闭包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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