javascript createAdder函數(shù)功能與使用說(shuō)明
更新時(shí)間:2010年06月04日 23:18:35 作者:
createAdder(x)是一個(gè)函數(shù),返回一個(gè)函數(shù)。在JavaScript中,函數(shù)是第一類對(duì)象:另外它們可以被傳遞到其他函數(shù)作為參數(shù)和函數(shù)返回。在這種情況下,函數(shù)返回本身就是一個(gè)函數(shù)接受一個(gè)參數(shù),并增加了一些東西。
英文原文
createAdder(x) is a function that returns a function. In JavaScript, functions are first-class objects: they can be passed to other functions as arguments and returned from functions as well. In this case, the function returned is itself a function that takes an argument and adds something to it.
Here’s the magic: the function returned by createAdder() is a closure. It “remembers” the environment in which it was created. If you pass createAdder the integer 3, you get back a function that will add 3 to its argument. If you pass 4, you get back a function that adds 4. The addThree and addFour functions in the above example are created in this way.
Let’s take another look at the addLoadEvent function. It takes as its argument a callback function which you wish to be executed once the page has loaded. There follow two cases: in the first case, window.onload does not already have a function assigned to it, so the function simply assigns the callback to window.onload. The second case is where the closure comes in: window.onload has already had something assigned to it. This previously assigned function is first saved in a variable called oldonload. Then a brand new function is created which first executes oldonload, then executes the new callback function. This new function is assigned to window.onload. Thanks to the magical property of closures, it will “remember” what the initial onload function was. Further more, you can call the addLoadEvent function multiple times with different arguments and it will build up a chain of functions, making sure that everything will be executed when the page loads no matter how many callbacks you have added.
Closures are a very powerful language feature but can take some getting used to. This article on Wikipedia provides more in-depth coverage.
中文翻譯:有更好的可以留言。大體意思差不多了
createAdder(x)是一個(gè)函數(shù),返回一個(gè)函數(shù)。在JavaScript中,函數(shù)是第一類對(duì)象:另外它們可以被傳遞到其他函數(shù)作為參數(shù)和函數(shù)返回。在這種情況下,函數(shù)返回本身就是一個(gè)函數(shù)接受一個(gè)參數(shù),并增加了一些東西。
在這里,Äôs the magic:由createAdder返回函數(shù)()是一個(gè)閉包。它,Äúremembers,非盟在創(chuàng)建它的環(huán)境。如果傳遞createAdder整數(shù)3,你回來(lái)一個(gè)函數(shù),將增加3至其參數(shù)。如果你通過(guò)四,你回來(lái)一個(gè)函數(shù),增加了4。該addThree在上面的例子addFour職能創(chuàng)造這樣的。
讓,星光大道可以再一次看看addLoadEvent功能。這需要將執(zhí)行一次頁(yè)面已加載為一個(gè)回調(diào)函數(shù)的參數(shù),你的愿望。有下列兩種情況:在第一種情況,在window.onload已經(jīng)沒(méi)有分配給它一個(gè)函數(shù),因此函數(shù)簡(jiǎn)單的回調(diào)在window.onload分配。第二個(gè)案例是在關(guān)閉的時(shí)候:在window.onload已經(jīng)有分配給它的東西。這是以前分配的功能首次在一個(gè)名為oldonload變量保存。然后,一個(gè)全新的功能是創(chuàng)建的第一個(gè)執(zhí)行oldonload,然后執(zhí)行新的回調(diào)函數(shù)。這一新功能被分配在window.onload。神奇的封鎖財(cái)產(chǎn)感謝,它會(huì)Äúremember,非盟最初的onload什么功能。進(jìn)一步,你可以調(diào)用函數(shù)的addLoadEvent多次與不同的參數(shù),它會(huì)建立一個(gè)職能鏈,確保一切都將在頁(yè)面加載時(shí)執(zhí)行,不管你有多少回調(diào)增加。
閉包是一個(gè)非常強(qiáng)大的語(yǔ)言功能,但可能需要一些時(shí)間來(lái)適應(yīng)。這種對(duì)維基百科的文章提供了更深入的報(bào)道。
核心代碼
function createAdder(x) {
return function(y) {
return y + x;
}
}
addThree = createAdder(3);
addFour = createAdder(4);
document.write('10 + 3 is ' + addThree(10) + '<BR>');
document.write('10 + 4 is ' + addFour(10));
document.write('-10 + 4 is ' + addFour(-10));
演示代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
createAdder(x) is a function that returns a function. In JavaScript, functions are first-class objects: they can be passed to other functions as arguments and returned from functions as well. In this case, the function returned is itself a function that takes an argument and adds something to it.
Here’s the magic: the function returned by createAdder() is a closure. It “remembers” the environment in which it was created. If you pass createAdder the integer 3, you get back a function that will add 3 to its argument. If you pass 4, you get back a function that adds 4. The addThree and addFour functions in the above example are created in this way.
Let’s take another look at the addLoadEvent function. It takes as its argument a callback function which you wish to be executed once the page has loaded. There follow two cases: in the first case, window.onload does not already have a function assigned to it, so the function simply assigns the callback to window.onload. The second case is where the closure comes in: window.onload has already had something assigned to it. This previously assigned function is first saved in a variable called oldonload. Then a brand new function is created which first executes oldonload, then executes the new callback function. This new function is assigned to window.onload. Thanks to the magical property of closures, it will “remember” what the initial onload function was. Further more, you can call the addLoadEvent function multiple times with different arguments and it will build up a chain of functions, making sure that everything will be executed when the page loads no matter how many callbacks you have added.
Closures are a very powerful language feature but can take some getting used to. This article on Wikipedia provides more in-depth coverage.
中文翻譯:有更好的可以留言。大體意思差不多了
createAdder(x)是一個(gè)函數(shù),返回一個(gè)函數(shù)。在JavaScript中,函數(shù)是第一類對(duì)象:另外它們可以被傳遞到其他函數(shù)作為參數(shù)和函數(shù)返回。在這種情況下,函數(shù)返回本身就是一個(gè)函數(shù)接受一個(gè)參數(shù),并增加了一些東西。
在這里,Äôs the magic:由createAdder返回函數(shù)()是一個(gè)閉包。它,Äúremembers,非盟在創(chuàng)建它的環(huán)境。如果傳遞createAdder整數(shù)3,你回來(lái)一個(gè)函數(shù),將增加3至其參數(shù)。如果你通過(guò)四,你回來(lái)一個(gè)函數(shù),增加了4。該addThree在上面的例子addFour職能創(chuàng)造這樣的。
讓,星光大道可以再一次看看addLoadEvent功能。這需要將執(zhí)行一次頁(yè)面已加載為一個(gè)回調(diào)函數(shù)的參數(shù),你的愿望。有下列兩種情況:在第一種情況,在window.onload已經(jīng)沒(méi)有分配給它一個(gè)函數(shù),因此函數(shù)簡(jiǎn)單的回調(diào)在window.onload分配。第二個(gè)案例是在關(guān)閉的時(shí)候:在window.onload已經(jīng)有分配給它的東西。這是以前分配的功能首次在一個(gè)名為oldonload變量保存。然后,一個(gè)全新的功能是創(chuàng)建的第一個(gè)執(zhí)行oldonload,然后執(zhí)行新的回調(diào)函數(shù)。這一新功能被分配在window.onload。神奇的封鎖財(cái)產(chǎn)感謝,它會(huì)Äúremember,非盟最初的onload什么功能。進(jìn)一步,你可以調(diào)用函數(shù)的addLoadEvent多次與不同的參數(shù),它會(huì)建立一個(gè)職能鏈,確保一切都將在頁(yè)面加載時(shí)執(zhí)行,不管你有多少回調(diào)增加。
閉包是一個(gè)非常強(qiáng)大的語(yǔ)言功能,但可能需要一些時(shí)間來(lái)適應(yīng)。這種對(duì)維基百科的文章提供了更深入的報(bào)道。
核心代碼
復(fù)制代碼 代碼如下:
function createAdder(x) {
return function(y) {
return y + x;
}
}
addThree = createAdder(3);
addFour = createAdder(4);
document.write('10 + 3 is ' + addThree(10) + '<BR>');
document.write('10 + 4 is ' + addFour(10));
document.write('-10 + 4 is ' + addFour(-10));
演示代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
您可能感興趣的文章:
相關(guān)文章
webpack4實(shí)現(xiàn)不同的導(dǎo)出類型
這篇文章主要介紹了webpack4實(shí)現(xiàn)不同的導(dǎo)出類型,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04js(JavaScript)實(shí)現(xiàn)TAB標(biāo)簽切換效果的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)js(JavaScript)實(shí)現(xiàn)TAB標(biāo)簽切換效果的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02利用JavaScript差集實(shí)現(xiàn)一個(gè)對(duì)比小工具
這篇文章主要給大家介紹了關(guān)于利用JavaScript差集實(shí)現(xiàn)一個(gè)對(duì)比小工具的相關(guān)資料,雖然實(shí)現(xiàn)的界面不是太好看,但好在功能實(shí)用即可,需要的朋友可以參考下2021-07-07javascript實(shí)現(xiàn)點(diǎn)擊圖片切換功能
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)點(diǎn)擊圖片切換功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07phantomjs導(dǎo)出html到pdf的方法總結(jié)
這篇文章主要介紹了phantomjs導(dǎo)出html到pdf的方法總結(jié),需要的朋友可以參考下2017-10-10ES2020讓代碼更優(yōu)美的運(yùn)算符 (?.) (??)
這篇文章主要介紹了ES2020讓代碼更優(yōu)美的運(yùn)算符 (?.) (??),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01JavaScript實(shí)現(xiàn)輪播圖方法(邏輯清晰一看就懂)
這篇文章主要給大家介紹了關(guān)于JavaScript實(shí)現(xiàn)輪播圖方法的相關(guān)資料,JS輪播圖的實(shí)現(xiàn)核心是使用JavaScript來(lái)控制圖片的切換和顯示,配合HTML和CSS完成布局和樣式設(shè)置,文中介紹的方法邏輯清晰一看就懂,需要的朋友可以參考下2023-12-12JS+CSS實(shí)現(xiàn)Li列表隔行換色效果的方法
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)Li列表隔行換色效果的方法,實(shí)例分析了js操作li節(jié)點(diǎn)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02