天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

javascript createAdder函數功能與使用說明

英文原文
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)是一個函數,返回一個函數。在JavaScript中,函數是第一類對象:另外它們可以被傳遞到其他函數作為參數和函數返回。在這種情況下,函數返回本身就是一個函數接受一個參數,并增加了一些東西。

在這里,Äôs the magic:由createAdder返回函數()是一個閉包。它,Äúremembers,非盟在創建它的環境。如果傳遞createAdder整數3,你回來一個函數,將增加3至其參數。如果你通過四,你回來一個函數,增加了4。該addThree在上面的例子addFour職能創造這樣的。

讓,星光大道可以再一次看看addLoadEvent功能。這需要將執行一次頁面已加載為一個回調函數的參數,你的愿望。有下列兩種情況:在第一種情況,在window.onload已經沒有分配給它一個函數,因此函數簡單的回調在window.onload分配。第二個案例是在關閉的時候:在window.onload已經有分配給它的東西。這是以前分配的功能首次在一個名為oldonload變量保存。然后,一個全新的功能是創建的第一個執行oldonload,然后執行新的回調函數。這一新功能被分配在window.onload。神奇的封鎖財產感謝,它會Äúremember,非盟最初的onload什么功能。進一步,你可以調用函數的addLoadEvent多次與不同的參數,它會建立一個職能鏈,確保一切都將在頁面加載時執行,不管你有多少回調增加。

閉包是一個非常強大的語言功能,但可能需要一些時間來適應。這種對維基百科的文章提供了更深入的報道。

核心代碼
復制代碼 代碼如下:
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需刷新才能執行]

JavaScript技術javascript createAdder函數功能與使用說明,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 最近中文字幕无吗免费高清 | 亚洲偷偷自拍免费视频在线 | 亚洲精品6久久久久中文字幕 | 日韩熟女精品一区二区三区 | 日韩精品一区二区三区色欲AV | 亚洲一卡久久4卡5卡6卡7卡 | 国产产一区二区三区久久毛片国语 | 国产午夜精品久久理论片小说 | 一边亲着一面膜下的免费过程 | 婷婷六月激情综合一区 | 嗯啊…嗯np男男双性总受 | 国产成人精品综合久久久 | 日本中文字幕伊人成中文字幕 | 精品国产乱码久久久久久夜深人妻 | 草草久久久无码国产专区全集观看 | 亚州日韩精品AV片无码中文 | 伊人久久伊人 | 午夜噜噜噜私人影院在线播放 | 日本bbwhd| 夜夜女人国产香蕉久久精品 | 四虎精品久久久久影院 | 午夜亚洲国产理论片二级港台二级 | 沟沟人体一区二区 | 小小水蜜桃视频高清在线观看免费 | 最近中文字幕无吗免费高清 | 亚洲日韩有码无线免费 | WWW国产精品内射老师 | 久久久无码精品无码国产人妻丝瓜 | 外国三级片名 | 一本道无码字幕在线看 | 纯肉高H种马艳遇风流多 | 蜜臀AV中文字幕熟女人妻 | 亚洲综合国产精品 | adc影院在线 | 欧美重口绿帽video | 公粗挺进了我的密道在线播放贝壳 | 色欲色香天天天综合 | 韩日午夜在线资源一区二区 | 精品伊人久久久 | 哒哒哒影院在线观看免费高清 | 中文字幕乱码亚洲无线三区 |