翻编程书籍的时候,往往都会提到回调函数,到底回调函数和普通的函数调用有什么区别,google和baidu了一大堆之后写一写自己的理解,中英文同时举例相信应该能够说明的更加清楚的


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是作为参数传入进去的,能够让一个高层的函数调用底层的函数



但是,没有把callback的实际应用场景说明白

举个简单的例子:

我现在很想买书,跑到网上各种找,都没有这本书,于是我在网上留下了我的联系方式,等到网上的书到货之后(用专业术语说是某个特定的事件发生之后,英文用event),然后工作人员给我打电话通知书到了(事件发生之后调用callback函数)你还买不买?

英文举例(要习惯于去看英文的解释,中文的很多翻译都不靠谱,包括我自己的)

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函数的目的是什么呢?个人认为是,因为我当前不知道我需要的那个事件什么时候发生(书什么时候到),而且我需要去做别的事情(不能一直傻傻的看着书店的货源看看我的书到没到),而是我自己正常做自己的事情,而等到书到了之后(事件已经发生了),有人自然会跟我打电话的(调用了回调函数)

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.


个人理解:callback和calling的区别是:

A:calling我知道什么时候调用的,而callback不知道(不知道什么时候函数执行,因为事件不知道什么时候发生,事件发生之后,函数执行,所以也可能函数不执行)

B:callback是被动调用的(事件发生之后才会执行),而calling是主动的

C:calling我知道调用的是哪个方法,而callback是不知道的(是用户传入的),在我买书的例子中,我想留下自己的联系方法,如果我留下的是QQ,工作人员需要调用我的QQ聊天的方法,如果我留下的是邮箱,工作人员需要调用发邮件的方法;如果我留下的是电话,需要调用打电话的方法。而这些方法,都应该是我(买书的用户)已经实现好的,而工作人员(调用者)只需要调用我的方法即可。

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).