国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

分享一個關于匿名函數和閉包的問答

Barrior / 3466人閱讀

摘要:引用一個的提問個人覺得總結的比較好的兩句話原文地址另外,附上中對閉包的講解閉包中文對于閉包的簡要概括原文原文地址匿名函數和閉包來自文章作者版權聲明自由轉載非商用非衍生保持署名創意共享許可證轉載請注明出處

引用一個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許可證) 轉載請注明出處

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/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元查看
<