Callback(回调)是什么?

Callback 是什么?

callback 是一种特殊的函数,这个函数被作为参数传给另一个函数去调用。这样的函数就是回调函数。

callback 拆开,就是 call back,在英语里面就是「回拨电话」的意思。

那我们就用打电话为例子来说明一下 callback:

  1. 「我打电话给某某」(I call somebody),那么「打电话」的人就是「我」。
  2. 「我」在电话里说:你办完某事后,回拨电话给「我」。
  3. 某某做完事后,就会「回拨电话给我」(calls back to me),那么「打电话」的人就是「某某」。

用编程来解释的话,是这样的:

  1. 「我调用一个函数 f」(I call a function),那么「调用函数」的人是「我」。代码是 f(c)。
  2. 「我」让这个函数 f 在执行完后,调用我传给它的另一个函数 c。
  3. f 执行完的时候,就会「调用 c」,也叫做「回调 c」(call c back),调用 c 的人是 f。

好了,解释完了:callback 就是(传给另一个函数调用的)函数。

把括号里面的内容去掉,简化成:callback 就是一种函数。

Callback 很常见

$button.on('click', function(){})

click 后面的 function 就是一个回调,因为「我」没有调用过这个函数,是 jQuery 在用户点击 button 时调用的。

div.addEventListener('click', function(){})

click 后面的 function 也是一个回调,因为「我」没有调用过这个函数,是浏览器在用户点击 button 时调用的。

一般来说,只要参数是一个函数,那么这个函数就是回调。

Callback 有点反直觉

很多初学者不明白 callback 的用法,因为 callback 有一点「反直觉」。
比如说我们用代码做一件事情,分为两步:step1( ) 和 step2( )。

符合人类直觉的代码是:

step1()
step2()

callback 的写法却是这样的:

step1(step2)

为什么要这样写?或者说在什么情况下应该用这个「反直觉」的写法?

一般(注意我说了一般),在 step1 是一个异步任务的时候,就会使用 callback。

什么是异步任务呢?且听下回分解。

作者:方应杭,想学前端?进群

64 thoughts on “Callback(回调)是什么?”

  1. Hey There. I found your blog using msn. This is a very well written article. I will make sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll certainly comeback.|

  2. May I just say what a comfort to discover an individual who really understands what they’re discussing on the web. You definitely know how to bring a problem to light and make it important. More and more people ought to check this out and understand this side of your story. It’s surprising you’re not more popular because you definitely have the gift.|

  3. fantastic points altogether, you simply gained a new reader. What would you recommend about your post that you simply made a few days ago? Any positive?|

  4. Hi, I do believe this is an excellent web site. I stumbledupon it 😉 I will return once again since I saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide other people.|

  5. Hmm is anyone else experiencing problems with the pictures on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.|

  6. It’s a pity you don’t have a donate button! I’d without a doubt donate to this superb blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this blog with my Facebook group. Chat soon!|

  7. Hey there I am so excited I found your blog, I really found you by accident, while I was browsing on Askjeeve for something else, Regardless I am here now and would just like to say many thanks for a incredible post and a all round exciting blog (I also love the theme/design), I don’t have time to read it all at the moment but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb work.|

  8. I’m really enjoying the design and layout of your website. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Exceptional work!|

  9. Thanks for one’s marvelous posting! I really enjoyed reading it, you happen to be a great author. I will make sure to bookmark your blog and will eventually come back down the road. I want to encourage yourself to continue your great work, have a nice holiday weekend!|

  10. I think this is one of the most vital info for me. And i am glad reading your article. But should remark on some general things, The site style is ideal, the articles is really great : D. Good job, cheers|

  11. Simply desire to say your article is as astounding. The clearness in your publish is simply nice and that i could suppose you’re a professional on this subject. Well along with your permission let me to grab your feed to keep updated with approaching post. Thank you a million and please continue the gratifying work.|

  12. Normally I don’t read post on blogs, but I would like to say that this write-up very forced me to take a look at and do so! Your writing taste has been amazed me. Thank you, quite nice post.|

  13. I like what you guys tend to be up too. Such clever work and reporting! Keep up the wonderful works guys I’ve included you guys to my blogroll.|

  14. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Bless you!|

  15. My family members every time say that I am killing my time here at web, but I know I am getting experience everyday by reading such good articles or reviews.|

  16. Link exchange is nothing else however it is just placing the other person’s webpage link on your page at appropriate place and other person will also do same in favor of you.|

  17. You can certainly see your enthusiasm within the article you write. The sector hopes for even more passionate writers such as you who are not afraid to mention how they believe. At all times follow your heart.|

  18. Yesterday, while I was at work, my sister stole my apple ipad and tested to see if it can survive a 25 foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is totally off topic but I had to share it with someone!|

  19. Awesome blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog stand out. Please let me know where you got your theme. Thanks|

  20. It is perfect time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or suggestions. Maybe you can write next articles referring to this article. I wish to read even more things about it!|

  21. If you are going for finest contents like I do, only pay a visit this web page all the time for the reason that it provides feature contents, thanks|

  22. Good day! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a extraordinary job!|

  23. Thanks for the good writeup. It in fact used to be a entertainment account it. Look advanced to far introduced agreeable from you! By the way, how could we be in contact?|

  24. If you are going for finest contents like I do, just pay a quick visit this website every day because it offers quality contents, thanks|

  25. Oh my goodness! Amazing article dude! Many thanks, However I am having issues with your RSS. I don’t understand why I am unable to join it. Is there anyone else getting the same RSS problems? Anyone that knows the answer will you kindly respond? Thanks!!|

  26. Excellent blog you have got here.. It’s hard to find quality writing like yours nowadays. I seriously appreciate individuals like you! Take care!!|

  27. Do you have a spam issue on this blog; I also am a blogger, and I was wanting to know your situation; we have created some nice methods and we are looking to exchange techniques with other folks, why not shoot me an e-mail if interested.|

  28. Nice blog here! Also your website loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as fast as yours lol|

  29. I think this is among the most vital information for me. And i’m glad reading your article. But wanna remark on few general things, The web site style is perfect, the articles is really great : D. Good job, cheers|

  30. Hello there, I believe your blog may be having internet browser compatibility issues. Whenever I take a look at your blog in Safari, it looks fine however, if opening in IE, it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Other than that, fantastic site!|

发表评论

电子邮件地址不会被公开。