麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 語言 > JavaScript > 正文

javascript createAdder函數功能與使用說明

2024-05-06 12:37:06
字體:
來源:轉載
供稿:網友
英文原文
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多次與不同的參數,它會建立一個職能鏈,確保一切都將在頁面加載時執行,不管你有多少回調增加。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 精品一区二区电影 | 亚洲精品欧美在线 | 免费久久久 | 欧美性生交zzzzzxxxxx | 狠狠干夜夜草 | 在线一级片 | av成人在线播放 | 圆产精品久久久久久久久久久 | 久久久国产精品网站 | 欧美成人午夜影院 | 一级α片免费看刺激高潮视频 | 韩国草草影院 | 黄色一级片免费在线观看 | 新久草在线视频 | 日韩色视频在线观看 | 久久蜜桃精品一区二区三区综合网 | 福利在线免费视频 | 青久草视频 | 国av在线| va视频在线 | 久久久久久久一区二区 | 香蕉黄色网 | 91精品国产综合久久久欧美 | 亚洲精品成人18久久久久 | 午夜视频国产 | 一区二区久久精品66国产精品 | 免费在线观看国产精品 | 欧美日韩在线视频观看 | 在线免费av观看 | 免费在线观看亚洲 | 曰韩av在线 | 久久国产精品久久久久久电车 | 精品一区二区在线观看视频 | 午夜天堂在线 | 圆产精品久久久久久久久久久 | 色偷偷一区 | 男男啪羞羞视频网站 | 在线播放黄色网址 | 免费男女乱淫真视频 | 国产精品一区网站 | 日韩视频观看 |