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

資訊專欄INFORMATION COLUMN

Javascript anonymous functions

劉福 / 1494人閱讀

Javascript anonymous functions
  

Anonymous functions are functions that are dynamically declared at runtime. They’re called anonymous functions because they aren’t given a name in the same way as normal functions.

  

Anonymous functions are declared using the function operator instead of the function declaration.

命名函數(shù):

function flyToTheMoon()
{
  alert("Zoom! Zoom! Zoom!");
}
flyToTheMoon();

匿名函數(shù):

var flyToTheMoon = function()
{
  alert("Zoom! Zoom! Zoom!");
}
flyToTheMoon();
Anonymous functions are created using the function operator

在JavaScript中創(chuàng)建函數(shù)最常用的有兩種方法:函數(shù)聲明和函數(shù)賦值表達式。匿名函數(shù)的創(chuàng)建使用的是函數(shù)賦值表達式。直接貼兩張圖,簡單明了:

函數(shù)聲明:

函數(shù)賦值表達式:

在調(diào)用函數(shù)賦值表達式(function operator)會創(chuàng)建一個新的函數(shù)對象,然后把返回它。這里就是把創(chuàng)建的函數(shù)對象賦值給flyToTheMoon。

這個賦值,跟把普通函數(shù)返回的值賦給變量是一樣的,比較特殊的就是匿名函數(shù)的值是一個函數(shù)對象而不是普通的如字符串或者時間這些變量。這可能是因為在JavaScript中函數(shù)就是一種特殊類型的對象,意味著你可以按操作其他對象一樣來操作它。它們能存儲在變量中,能作為參數(shù)傳遞給其它函數(shù),也可以通過return語句從其它函數(shù)里返回。函數(shù)總是對象,無論你是怎么創(chuàng)建的。

Anonymous functions are created at runtime
  

The function operator can be used anywhere that it’s valid to use an
expression. For example you can use the function operator when a
variable is being assigned, when a parameter is being passed to a
function or in a return statement. This is possible because the
function operator is always invoked at runtime.

Function declarations are different. They are run before any of the
other code is executed so the functions do not have to be declared
before the code that calls them.

Function declarations can’t be used to create anonymous functions
because they require the function to have a name. The function
declaration uses the function name to add it as a variable in the
current scope.

Anonymous functions don’t have a name

匿名函數(shù)沒有函數(shù)名,那怎么能調(diào)用呢?可以調(diào)用是因為函數(shù)名存的是一個函數(shù)對象,而不像普通變量。

函數(shù)聲明創(chuàng)建函數(shù)時,總是有一個函數(shù)名,還有一個有編譯器自動生成的和函數(shù)名的同名變量。

對于函數(shù)賦值表達式創(chuàng)建函數(shù)時,函數(shù)名是可選的。很多情況下,創(chuàng)建一個匿名函數(shù)時,函數(shù)名對我們來說是不重要的。就像這樣:

var flyToTheMoon = function() {
   alert("Zoom! Zoom! Zoom");
}
flyToTheMoon();

不過使用函數(shù)賦值表達式也可以設(shè)置函數(shù)名,這個函數(shù)和上面這個函數(shù)一模一樣,只是多了個函數(shù)名而已:

var flyToTheMoon = function flyToTheMoon() {
   alert("Zoom! Zoom! Zoom");
}
flyToTheMoon();
  

Giving your function a name does not automatically add a variable into scope with the function name. You still need to assign the return value of the function operator a variable.

上面這個例子中,函數(shù)名和存儲函數(shù)對象的變量名是一樣的,其實變量名也可以和函數(shù)名不一樣,如:

var thingsToDoToday = function flyToTheMoon() {
   alert("Zoom! Zoom! Zoom");
}
thingsToDoToday();
Why have a name?

添加一個函數(shù)名,看起來多余,其實還是很有用的,比如在遞歸函數(shù)中,可以通過函數(shù)名調(diào)用自身,如下:

var thingsToDoToday = function flyToTheMoon() {
  if(!onTheMoon)
    flyToTheMoon();
  else
    alert("One small step for a man..");
}
thingsToDoToday();
  

It can also useful for debugging because you can see the function’s
name in a call stack. Anonymous functions generally all look the same in the call stack. If you have a nasty debugging situation, sometimes giving names to the functions you are interested in can make things clearer

Why are anonymous functions useful?

Not having to set a name for an anonymous function is just a convenience thing since in most cases the name of the function doesn’t really matter. Most of the time anonymous functions and named functions will both do any job perfectly well.

Functions created with the function operator can be very useful. See some examples of where it can be used.

本文截取自:http://helephant.com/2008/08/23/javascript-anonymous-functions/

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

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

相關(guān)文章

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

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

    Barrior 評論0 收藏0
  • JavaScript陰溝里翻船之運算符優(yōu)先級

    摘要:操作符的兩種形態(tài)其實在的操作符描述中,語法是你會發(fā)現(xiàn)被中括號所包圍也就意味著可缺省,因此,如果對于不含參數(shù)的構(gòu)造函數(shù)而言與二者并無區(qū)別,那我們接著思考一個問題,對于前面返回函數(shù)的而言,當(dāng)?shù)臅r候為什么執(zhí)行的是而不是呢。  首先歡迎大家關(guān)注我的Github博客,也算是對我的一點鼓勵,畢竟寫東西沒法變現(xiàn),堅持下去也是靠的是自己的熱情和大家的鼓勵。各位讀者的Star是激勵我前進的動力,請不要吝...

    selfimpr 評論0 收藏0
  • 淺談Javascript閉包和匿名函數(shù)【1】

    摘要:我們可以用普通函數(shù)內(nèi)部嵌套匿名函數(shù),形成一個閉包來使變量駐留在內(nèi)存中。局部變量閉包為什么要將賦值給變量呢這里我們就要談到匿名函數(shù)調(diào)用問題匿名函數(shù)如何調(diào)用還是上面的例子會將整個函數(shù)體打印出來這樣才調(diào)用了函數(shù)內(nèi)部的匿名函數(shù)看到這里。 閉包含義: 閉包是指有權(quán)訪問另一個函數(shù)作用域中的變量的函數(shù),創(chuàng)建閉包的常見的方式,就是在一個函數(shù)內(nèi)部創(chuàng)建另一個函數(shù),通過另一個函數(shù)訪問這個函數(shù)的局部變量。 這...

    cyqian 評論0 收藏0
  • Elm入門實踐(二)——類型篇

    摘要:如果不聲明類型呢如果注釋掉類型注解重新編譯,還是會報錯,只是錯誤信息變了,這次是第行即使沒有顯式的類型注解,的類型推導(dǎo)系統(tǒng)也會發(fā)揮作用,此處通過類型推導(dǎo)認(rèn)為函數(shù)的參數(shù)應(yīng)該是字符串,但是傳入了數(shù)字,因此報錯。 記得Facebook曾經(jīng)在一次社區(qū)活動上說過,隨著他們越來越多地使用Javascript,很快就面臨了曾經(jīng)在PHP上遇到的問題:這東西到底是啥? 動態(tài)語言就像把雙刃劍,你可以愛死它...

    ZHAO_ 評論0 收藏0
  • ES6 Symbol ,對象匿名(anonymous)屬性實現(xiàn)

    摘要:在運行時環(huán)境中,通過調(diào)用函數(shù)創(chuàng)建值,該函數(shù)動態(tài)生成匿名的唯一值。創(chuàng)建和使用值的唯一創(chuàng)建方法,是通過調(diào)用函數(shù)來返回,不支持操作。共享體系提供了一個全局注冊表,用于在大文件或多文件代碼中追蹤值。 Symbol由來 Symbol是ES6引入的新類型,所以在ES5的基礎(chǔ)上,JS就有了字符串(string)、數(shù)字型(number)、布爾(bool)、null、undefined和Symbol共六...

    JowayYoung 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<