亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

分享一個(gè)關(guān)于匿名函數(shù)和閉包的問(wèn)答

Barrior / 3535人閱讀

摘要:引用一個(gè)的提問(wèn)個(gè)人覺得總結(jié)的比較好的兩句話原文地址另外,附上中對(duì)閉包的講解閉包中文對(duì)于閉包的簡(jiǎn)要概括原文原文地址匿名函數(shù)和閉包來(lái)自文章作者版權(quán)聲明自由轉(zhuǎn)載非商用非衍生保持署名創(chuàng)意共享許可證轉(zhuǎn)載請(qǐng)注明出處

引用一個(gè)stackoverflow的提問(wèn)

個(gè)人覺得總結(jié)的比較好的兩句話: "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中對(duì)閉包的講解:

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

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

Dmitry Soshnikov對(duì)于閉包的簡(jiǎn)要概括

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.

原文

原文地址:匿名函數(shù)和閉包(來(lái)自stackoverflow)
文章作者:zdying
版權(quán)聲明:自由轉(zhuǎn)載-非商用-非衍生-保持署名(創(chuàng)意共享3.0許可證) 轉(zhuǎn)載請(qǐng)注明出處

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/91148.html

相關(guān)文章

  • 關(guān)于個(gè)人第一天前端面試面試問(wèn)答QA,希望能對(duì)其他找前端工作朋友有所幫助。

    摘要:兩日前,發(fā)了一篇吐槽,莫名的火了一把。關(guān)于的第一個(gè),其實(shí)就是聲明一個(gè)常量,不允許變更。另外對(duì)象迭代這里出自,阮一峰大神寫的入門指南,對(duì)象篇。 兩日前,發(fā)了一篇吐槽,莫名的火了一把。經(jīng)過(guò)大家的建議與鼓勵(lì),于是修改了簡(jiǎn)歷,開始了重新投遞,2天后接到第一份面試邀請(qǐng)。 此文為個(gè)人面試經(jīng)歷,QA問(wèn)答過(guò)程與總結(jié),不透露面試公司及面試人員,內(nèi)容真實(shí),如果有面試過(guò)我的大佬看到博客,歡迎指出問(wèn)題。 循序...

    Youngdze 評(píng)論0 收藏0
  • Javascript動(dòng)態(tài)作用域

    摘要:獲取返回的匿名函數(shù)這個(gè)匿名函數(shù)會(huì)保留原本的作用域鏈有意思的是函數(shù)參數(shù)也是可以被我們捕獲到的這就給我們靈活的創(chuàng)造一些函數(shù)提供了便利,比如我們需要?jiǎng)?chuàng)造一個(gè)函數(shù)工廠,這個(gè)工廠可以根據(jù)我們提供的參數(shù)生產(chǎn)出不同的函數(shù)。 本文是在看《Javascript函數(shù)式》編程一書寫下的一些記錄。和大家分享。不足之處還望大家指正。 關(guān)于this的討論 首先來(lái)看這么幾段代碼 function globalThi...

    DC_er 評(píng)論0 收藏0
  • 前端基礎(chǔ)進(jìn)階(七):函數(shù)函數(shù)式編程

    摘要:一函數(shù)聲明函數(shù)表達(dá)式匿名函數(shù)與自執(zhí)行函數(shù)關(guān)于函數(shù)在實(shí)際開發(fā)中的應(yīng)用,大體可以總結(jié)為函數(shù)聲明函數(shù)表達(dá)式匿名函數(shù)自執(zhí)行函數(shù)。而匿名函數(shù),顧名思義,就是指的沒(méi)有被顯示進(jìn)行賦值操作的函數(shù)。而函數(shù)自執(zhí)行,其實(shí)是匿名函數(shù)的一種應(yīng)用。 showImg(https://segmentfault.com/img/remote/1460000008448954); 縱觀JavaScript中所有必須需要掌...

    GeekGhc 評(píng)論0 收藏0
  • 微信小程序開發(fā)教程(基礎(chǔ)篇)4-關(guān)于回調(diào)函數(shù),匿名函數(shù)閉包雜談

    摘要:而回調(diào)函數(shù)通常只是提供給其它模塊進(jìn)行調(diào)用,為了簡(jiǎn)化編碼,后續(xù)的等腳本語(yǔ)言中提供了對(duì)匿名函數(shù)的支持。當(dāng)使用回調(diào)函數(shù)時(shí),通常會(huì)涉及到一些上下文的傳遞。 嚴(yán)格來(lái)說(shuō),這不能算是一篇微信小程序教程,不過(guò)會(huì)使用到上一篇中的app.js代碼作為示例,姑且充個(gè)數(shù)吧。 回調(diào)函數(shù) 回調(diào)函數(shù),對(duì)于初入編程這一行的同學(xué)可能會(huì)有些難以理解,畢竟回調(diào)函數(shù)的使用和程序順序執(zhí)行的直觀流程是相悖的。 想象你定了一個(gè)外賣...

    shixinzhang 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<