翻編程書籍的時(shí)候,往往都會(huì)提到回調(diào)函數(shù),到底回調(diào)函數(shù)和普通的函數(shù)調(diào)用有什么區(qū)別,google和baidu了一大堆之后寫一寫自己的理解,中英文同時(shí)舉例相信應(yīng)該能夠說明的更加清楚的
what is callback function?
quote from wikipedia page:
In computer PRogramming, a callback is areference to executable code, or a piece of executable code, that is passed asan argument to other code. This allows a lower-level software layer to call asubroutine (or function) defined in a higher-level layer.
這是維基百科上的解釋,主要說明了callback是作為參數(shù)傳入進(jìn)去的,能夠讓一個(gè)高層的函數(shù)調(diào)用底層的函數(shù)但是,沒有把callback的實(shí)際應(yīng)用場景說明白
舉個(gè)簡單的例子:
我現(xiàn)在很想買書,跑到網(wǎng)上各種找,都沒有這本書,于是我在網(wǎng)上留下了我的聯(lián)系方式,等到網(wǎng)上的書到貨之后(用專業(yè)術(shù)語說是某個(gè)特定的事件發(fā)生之后,英文用event),然后工作人員給我打電話通知書到了(事件發(fā)生之后調(diào)用callback函數(shù))你還買不買?
英文舉例(要習(xí)慣于去看英文的解釋,中文的很多翻譯都不靠譜,包括我自己的)
I am going to try to keep this deadsimple. A "callback" is any function that is called by anotherfunction which takes the first function as a parameter. A lot of the time, a"callback" is a function that is called when something happens. That something can be called an "event" inprogrammer-speak.
Imagine this scenario: You are expecting a package in acouple of days. The package is a gift for your neighbor. Therefore, once youget the package, you want it brought over to the neighbors. You are out oftown, and so you leave instructions for your spouse.
You could tell them to get the package and bring it tothe neighbors. If your spouse was as stupid as a computer, they would sit atthe door and wait for the package until it came (NOT DOING ANYTHING ELSE) andthen once it came they would bring it over to the neighbors. But there's abetter way. Tell your spouse that ONCE they receive the package, they shouldbring it over the neighbors. Then, they can go about life normally UNTIL shereceives the package.
In our example, the receiving of thepackage is the "event" and the bringing it to the neighbors is the"callback". Your spouse "runs" your instructions to bringthe package over only when thepackage arrives. Much better!
那么,我們使用callback函數(shù)的目的是什么呢?個(gè)人認(rèn)為是,因?yàn)槲耶?dāng)前不知道我需要的那個(gè)事件什么時(shí)候發(fā)生(書什么時(shí)候到),而且我需要去做別的事情(不能一直傻傻的看著書店的貨源看看我的書到?jīng)]到),而是我自己正常做自己的事情,而等到書到了之后(事件已經(jīng)發(fā)生了),有人自然會(huì)跟我打電話的(調(diào)用了回調(diào)函數(shù))
Why would you want to do this? Let's say thereis a service you need to invoke. If the service returns immediately, you just:
Call it
Wait for the result
Continue once the result comes in
Just understand that it is just a name todescribe a method that's supplied as an argument to another method, such thatwhen the parent method is called (whatever condition, such as a button click, atimer tick etc) and its method body completes, the callback method is theninvoked, or in other Words "called at the back" of the other function.
個(gè)人理解:callback和calling的區(qū)別是:
A:calling我知道什么時(shí)候調(diào)用的,而callback不知道(不知道什么時(shí)候函數(shù)執(zhí)行,因?yàn)槭录恢朗裁磿r(shí)候發(fā)生,事件發(fā)生之后,函數(shù)執(zhí)行,所以也可能函數(shù)不執(zhí)行)
B:callback是被動(dòng)調(diào)用的(事件發(fā)生之后才會(huì)執(zhí)行),而calling是主動(dòng)的
C:calling我知道調(diào)用的是哪個(gè)方法,而callback是不知道的(是用戶傳入的),在我買書的例子中,我想留下自己的聯(lián)系方法,如果我留下的是QQ,工作人員需要調(diào)用我的QQ聊天的方法,如果我留下的是郵箱,工作人員需要調(diào)用發(fā)郵件的方法;如果我留下的是電話,需要調(diào)用打電話的方法。而這些方法,都應(yīng)該是我(買書的用戶)已經(jīng)實(shí)現(xiàn)好的,而工作人員(調(diào)用者)只需要調(diào)用我的方法即可。
How are they different from calling onefunction from another function taking some context from the calling function?
It is true that you are calling a functionfrom another function, but the key is that the callback is treated like anObject, so you can change which Function to call based on the state of thesystem (like the Strategy Design Pattern).
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注