博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分享一个关于匿名函数和闭包的问答
阅读量:7250 次
发布时间:2019-06-29

本文共 2718 字,大约阅读时间需要 9 分钟。

引用一个stackoverflow的提问

个人觉得总结的比较好的两句话: "An anonymous function is just a function that has no name; nothing more. A closure is a function that captures the state of the surrounding environment."

A:

Hi,

I have been unable to find a definition that clearly explains the differences between a closure and an anonymous function.

Most references I have seen clearly specify that they are distinct "things" yet I can't seem to get my head around why.

Could someone please simplify it for me? What are the specific differences between these two language features? Which one is more appropriate in what scenarios?

Q:

An anonymous function is just a function that has no name; nothing more. A closure is a function that captures the state of the surrounding environment.

An anonymous function does not necessarily need to create a closure, and a closure is not created only for anonymous functions.

Consider this hypothetical counter-example. Consider a language Foo which does not support closures but supports anonymous functions. This language may either not compile or throw an error for the code below because "greeting" is not defined in the scope of the inner function. The fact that it is anonymous is irrelevant.

function outer() {    var greeting = "hello ";    (function(name) {        alert(greeting + name);    })("John Doe");}

Let's consider an actual language now that does support closures - JavaScript. Taking the same example as above, but naming the inner function this time gives:

function outer() {    var greeting = "hello ";    (function inner(name) {        alert(greeting + name);    })("John Doe");}

Although the inner function is not anonymous anymore, it still captures state from the surrounding environment.

Closures provide much needed convenience, as otherwise we would be passing every single dependency of the function as an argument.

function outer() {    var greeting = "hello ";    (function(name, greeting) {        alert(greeting + name);    })("John Doe", greeting);}

原文地址:

另外,附上MDN中对闭包的讲解:

Closures - JavaScript(English):

闭包 - JavaScript(中文):

Dmitry Soshnikov对于闭包的简要概括

Closures in ECMAScript are directly related with the [[Scope]] property of functions. As it has been noted, [[Scope]] is saved at function creation and exists until the function object is destroyed. Actually, a closure is exactly a combination of a function code and its [[Scope]] property. Thus, [[Scope]] contains that lexical environment (the parent variable object) in which function is created. Variables from higher contexts at the further function activation will be searched in this lexical (statically saved at creation) chain of variable objects.

原文

原文地址:

文章作者:zdying
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证) 转载请注明出处

你可能感兴趣的文章
Spring @Transactional propagation 各个属性值的含义
查看>>
知乎背景图 canvas 效果
查看>>
Vncserver安装-Centos7
查看>>
div自身高度、屏幕高度
查看>>
HDU 1358 Period(kmp简单解决)
查看>>
PYTHON压平嵌套列表
查看>>
JavaScript -- BATweb笔试面试
查看>>
halcon算子翻译——compose7
查看>>
关于angular 自定义directive
查看>>
第五章 Python 函数(一)
查看>>
无聊的数列 线段树差分
查看>>
Dart: path库
查看>>
uva 10739【基础(区间)dp】
查看>>
PHP json_encode() 函数介绍
查看>>
武汉科技大学ACM :1009: 华科版C语言程序设计教程(第二版)习题6.11
查看>>
判断是否为时间格式
查看>>
电影池子,
查看>>
OVN QoS
查看>>
Browser Window
查看>>
p2944 [USACO09MAR]地震损失2Earthquake Damage 2
查看>>