资讯专栏INFORMATION COLUMN

分享一个关于匿名函数和闭包的问答

Barrior / 3376人阅读

摘要:引用一个的提问个人觉得总结的比较好的两句话原文地址另外,附上中对闭包的讲解闭包中文对于闭包的简要概括原文原文地址匿名函数和闭包来自文章作者版权声明自由转载非商用非衍生保持署名创意共享许可证转载请注明出处

引用一个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);
}

原文地址: http://stackoverflow.com/ques...

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

Closures - JavaScript(English): http://developer.mozilla.org/...

闭包 - JavaScript(中文): http://developer.mozilla.org/...

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.

原文

原文地址:匿名函数和闭包(来自stackoverflow)
文章作者:zdying
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证) 转载请注明出处

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/91148.html

相关文章

  • 关于个人第一天前端面试面试问答QA,希望能对其他找前端工作朋友有所帮助。

    摘要:两日前,发了一篇吐槽,莫名的火了一把。关于的第一个,其实就是声明一个常量,不允许变更。另外对象迭代这里出自,阮一峰大神写的入门指南,对象篇。 两日前,发了一篇吐槽,莫名的火了一把。经过大家的建议与鼓励,于是修改了简历,开始了重新投递,2天后接到第一份面试邀请。 此文为个人面试经历,QA问答过程与总结,不透露面试公司及面试人员,内容真实,如果有面试过我的大佬看到博客,欢迎指出问题。 循序...

    Youngdze 评论0 收藏0
  • Javascript动态作用域

    摘要:获取返回的匿名函数这个匿名函数会保留原本的作用域链有意思的是函数参数也是可以被我们捕获到的这就给我们灵活的创造一些函数提供了便利,比如我们需要创造一个函数工厂,这个工厂可以根据我们提供的参数生产出不同的函数。 本文是在看《Javascript函数式》编程一书写下的一些记录。和大家分享。不足之处还望大家指正。 关于this的讨论 首先来看这么几段代码 function globalThi...

    DC_er 评论0 收藏0
  • 前端基础进阶(七):函数函数式编程

    摘要:一函数声明函数表达式匿名函数与自执行函数关于函数在实际开发中的应用,大体可以总结为函数声明函数表达式匿名函数自执行函数。而匿名函数,顾名思义,就是指的没有被显示进行赋值操作的函数。而函数自执行,其实是匿名函数的一种应用。 showImg(https://segmentfault.com/img/remote/1460000008448954); 纵观JavaScript中所有必须需要掌...

    GeekGhc 评论0 收藏0
  • 微信小程序开发教程(基础篇)4-关于回调函数匿名函数闭包杂谈

    摘要:而回调函数通常只是提供给其它模块进行调用,为了简化编码,后续的等脚本语言中提供了对匿名函数的支持。当使用回调函数时,通常会涉及到一些上下文的传递。 严格来说,这不能算是一篇微信小程序教程,不过会使用到上一篇中的app.js代码作为示例,姑且充个数吧。 回调函数 回调函数,对于初入编程这一行的同学可能会有些难以理解,毕竟回调函数的使用和程序顺序执行的直观流程是相悖的。 想象你定了一个外卖...

    shixinzhang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<