一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

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

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

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

核心代碼
復(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需刷新才能執(zhí)行]

JavaScript技術(shù)javascript createAdder函數(shù)功能與使用說明,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 免费成人在线观看视频 | 欧美第一页草草影院浮力 | 成人精品人成网站 | 亚洲日本欧美产综合在线 | 国产精品久久久久亚洲 | 久久精品视 | 日本三级成人中文字幕乱码 | 色老大综合久久综合热 | 国产一区二区三区久久精品 | 日韩美女色高清在线看 | 国产精品美女网站在线看 | 色吊丝欧美 | 欧美另类videosbestsex亚洲 | 91精品国产免费自在线观看 | 国产亚洲高清在线精品99 | www.色网站 | 国产一区二区三区手机在线观看 | 欧美色欧美亚洲高清在线观看 | 国产成人麻豆精品video | 色综合色狠狠天天久久婷婷基地 | 多人伦交性欧美在线观看 | 免费超爽大片黄网站 | 欧美一区色 | 在线视频精品一区 | 国产成人免费午夜在线观看 | 久久久久久91精品色婷婷 | 91国内揄拍·国内精品对白 | 亚洲热在线 | 理论片日韩 | 99久久精品国内 | 亚洲一区视频在线播放 | 青热久思思 | 国产欧美视频综合二区 | 欧美高清在线观看视频 | 亚洲成综合人影院在院播放 | 男人女人的免费视频网站 | 亚洲欧美日本一区 | 黄视频网站在线观看 | 四虎最新永久在线精品免费 | 亚洲综合婷婷 | 激情5月婷婷 |