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ù)賦值表達(dá)式。匿名函數(shù)的創(chuàng)建使用的是函數(shù)賦值表達(dá)式。直接貼兩張圖,簡單明了:
函數(shù)聲明:
函數(shù)賦值表達(dá)式:
在調(diào)用函數(shù)賦值表達(dá)式(function operator)會創(chuàng)建一個(gè)新的函數(shù)對象,然后把返回它。這里就是把創(chuàng)建的函數(shù)對象賦值給flyToTheMoon。
這個(gè)賦值,跟把普通函數(shù)返回的值賦給變量是一樣的,比較特殊的就是匿名函數(shù)的值是一個(gè)函數(shù)對象而不是普通的如字符串或者時(shí)間這些變量。這可能是因?yàn)樵贘avaScript中函數(shù)就是一種特殊類型的對象,意味著你可以按操作其他對象一樣來操作它。它們能存儲在變量中,能作為參數(shù)傳遞給其它函數(shù),也可以通過return語句從其它函數(shù)里返回。函數(shù)總是對象,無論你是怎么創(chuàng)建的。
Anonymous functions are created at runtimeAnonymous functions don’t have a nameThe 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.
匿名函數(shù)沒有函數(shù)名,那怎么能調(diào)用呢?可以調(diào)用是因?yàn)楹瘮?shù)名存的是一個(gè)函數(shù)對象,而不像普通變量。
函數(shù)聲明創(chuàng)建函數(shù)時(shí),總是有一個(gè)函數(shù)名,還有一個(gè)有編譯器自動(dòng)生成的和函數(shù)名的同名變量。
對于函數(shù)賦值表達(dá)式創(chuàng)建函數(shù)時(shí),函數(shù)名是可選的。很多情況下,創(chuàng)建一個(gè)匿名函數(shù)時(shí),函數(shù)名對我們來說是不重要的。就像這樣:
var flyToTheMoon = function() { alert("Zoom! Zoom! Zoom"); } flyToTheMoon();
不過使用函數(shù)賦值表達(dá)式也可以設(shè)置函數(shù)名,這個(gè)函數(shù)和上面這個(gè)函數(shù)一模一樣,只是多了個(gè)函數(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.
上面這個(gè)例子中,函數(shù)名和存儲函數(shù)對象的變量名是一樣的,其實(shí)變量名也可以和函數(shù)名不一樣,如:
var thingsToDoToday = function flyToTheMoon() { alert("Zoom! Zoom! Zoom"); } thingsToDoToday();Why have a name?
添加一個(gè)函數(shù)名,看起來多余,其實(shí)還是很有用的,比如在遞歸函數(shù)中,可以通過函數(shù)名調(diào)用自身,如下:
var thingsToDoToday = function flyToTheMoon() { if(!onTheMoon) flyToTheMoon(); else alert("One small step for a man.."); } thingsToDoToday();
Why are anonymous functions useful?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
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://systransis.cn/yun/85405.html
摘要:引用一個(gè)的提問個(gè)人覺得總結(jié)的比較好的兩句話原文地址另外,附上中對閉包的講解閉包中文對于閉包的簡要概括原文原文地址匿名函數(shù)和閉包來自文章作者版權(quán)聲明自由轉(zhuǎn)載非商用非衍生保持署名創(chuàng)意共享許可證轉(zhuǎn)載請注明出處 引用一個(gè)stackoverflow的提問 個(gè)人覺得總結(jié)的比較好的兩句話: An anonymous function is just a function that has no na...
摘要:操作符的兩種形態(tài)其實(shí)在的操作符描述中,語法是你會發(fā)現(xiàn)被中括號所包圍也就意味著可缺省,因此,如果對于不含參數(shù)的構(gòu)造函數(shù)而言與二者并無區(qū)別,那我們接著思考一個(gè)問題,對于前面返回函數(shù)的而言,當(dāng)?shù)臅r(shí)候?yàn)槭裁磮?zhí)行的是而不是呢。 首先歡迎大家關(guān)注我的Github博客,也算是對我的一點(diǎn)鼓勵(lì),畢竟寫東西沒法變現(xiàn),堅(jiān)持下去也是靠的是自己的熱情和大家的鼓勵(lì)。各位讀者的Star是激勵(lì)我前進(jìn)的動(dòng)力,請不要吝...
摘要:我們可以用普通函數(shù)內(nèi)部嵌套匿名函數(shù),形成一個(gè)閉包來使變量駐留在內(nèi)存中。局部變量閉包為什么要將賦值給變量呢這里我們就要談到匿名函數(shù)調(diào)用問題匿名函數(shù)如何調(diào)用還是上面的例子會將整個(gè)函數(shù)體打印出來這樣才調(diào)用了函數(shù)內(nèi)部的匿名函數(shù)看到這里。 閉包含義: 閉包是指有權(quán)訪問另一個(gè)函數(shù)作用域中的變量的函數(shù),創(chuàng)建閉包的常見的方式,就是在一個(gè)函數(shù)內(nèi)部創(chuàng)建另一個(gè)函數(shù),通過另一個(gè)函數(shù)訪問這個(gè)函數(shù)的局部變量。 這...
摘要:如果不聲明類型呢如果注釋掉類型注解重新編譯,還是會報(bào)錯(cuò),只是錯(cuò)誤信息變了,這次是第行即使沒有顯式的類型注解,的類型推導(dǎo)系統(tǒng)也會發(fā)揮作用,此處通過類型推導(dǎo)認(rèn)為函數(shù)的參數(shù)應(yīng)該是字符串,但是傳入了數(shù)字,因此報(bào)錯(cuò)。 記得Facebook曾經(jīng)在一次社區(qū)活動(dòng)上說過,隨著他們越來越多地使用Javascript,很快就面臨了曾經(jīng)在PHP上遇到的問題:這東西到底是啥? 動(dòng)態(tài)語言就像把雙刃劍,你可以愛死它...
摘要:在運(yùn)行時(shí)環(huán)境中,通過調(diào)用函數(shù)創(chuàng)建值,該函數(shù)動(dòng)態(tài)生成匿名的唯一值。創(chuàng)建和使用值的唯一創(chuàng)建方法,是通過調(diào)用函數(shù)來返回,不支持操作。共享體系提供了一個(gè)全局注冊表,用于在大文件或多文件代碼中追蹤值。 Symbol由來 Symbol是ES6引入的新類型,所以在ES5的基礎(chǔ)上,JS就有了字符串(string)、數(shù)字型(number)、布爾(bool)、null、undefined和Symbol共六...
閱讀 2880·2021-11-11 10:58
閱讀 1934·2021-10-11 10:59
閱讀 3500·2019-08-29 16:23
閱讀 2349·2019-08-29 11:11
閱讀 2797·2019-08-28 17:59
閱讀 3847·2019-08-27 10:56
閱讀 2093·2019-08-23 18:37
閱讀 3123·2019-08-23 16:53