国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

javascriptcreateAdder函數(shù)功能與使用說(shuō)明_javascript技巧

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 20:49:47
文檔

javascriptcreateAdder函數(shù)功能與使用說(shuō)明_javascript技巧

javascriptcreateAdder函數(shù)功能與使用說(shuō)明_javascript技巧:英文原文 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
推薦度:
導(dǎo)讀javascriptcreateAdder函數(shù)功能與使用說(shuō)明_javascript技巧:英文原文 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

英文原文
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ù)是第一類(lèi)對(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)造這樣的。

讓?zhuān)枪獯蟮揽梢栽僖淮慰纯碼ddLoadEvent功能。這需要將執(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) + '
');
document.write('10 + 4 is ' + addFour(10));
document.write('-10 + 4 is ' + addFour(-10));

演示代碼:

[Ctrl+A 全選 注:如需引入外部Js需刷新才能執(zhí)行]

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

javascriptcreateAdder函數(shù)功能與使用說(shuō)明_javascript技巧

javascriptcreateAdder函數(shù)功能與使用說(shuō)明_javascript技巧:英文原文 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
推薦度:
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top 主站蜘蛛池模板: 在线精品国产一区二区 | 免费国产精品视频 | 欧美亚洲综合在线观看 | zozozo欧美人禽交另类视频 | 久久国产精品成人免费 | 一级一级特黄女人精品毛片视频 | 麻豆啪啪 | 国产伦精品一区二区三区 | 日韩专区亚洲综合久久 | 国产在线精彩视频 | 永久免费毛片 | 性色a v 一区 | 久久精品一区 | 日本久热 | 欧美人善交 | 欧美在线日韩在线 | 91精品一区二区三区在线观看 | 天码毛片一区二区三区入口 | 久久精品123 | 福利一区福利二区 | 日韩欧美第一页 | 国产在线一区二区三区四区 | 国产一区在线观看视频 | 久久一区二区精品综合 | 国产精品不卡在线观看 | 日韩网 | 亚洲欧美日韩综合网导航 | 欧洲日韩视频二区在线 | 欧美一区二区三区精品 | 国产99久久九九精品免费 | 精品国产一区二区三区成人 | 最新大黄网站免费 | 国产一二三区在线观看 | 劲爆欧美第一页 | 在线看精品 | 欧美在线视频网站 | 欧美日韩视频一区二区三区 | 亚洲精品在线免费观看视频 | 国产一区二区三区视频 | 国产a级网站 | 国产观看在线 |