What is a callback function?

前端 未结 21 2417
耶瑟儿~
耶瑟儿~ 2020-11-21 23:01

What is a callback function?

21条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 23:36

    Let's keep it simple. What is a call back function?

    Example by Parable and Analogy

    I have a secretary. Everyday I ask her to: (i) drop off the firm's outgoing mail at the post office, and after she's done that, to do: (ii) whatever task I wrote for her on one of those sticky notes.

    Now, what is the task on the sticky-note? The task varies from day to day.

    Suppose on this particular day, I require her to print off some documents. So I write that down on the sticky note, and I pin it on her desk along with the outgoing mail she needs to post.

    In summary:

    1. first, she needs to drop off the mail and
    2. immediately after that is done, she needs to print off some documents.

    The call back function is that second task: printing off those documents. Because it is done AFTER the mail is dropped off, and also because the sticky note telling her to print the document is given to her along with the mail she needs to post.

    Let's now tie this in with programming vocabulary

    • The method name in this case is: DropOffMail.
    • And the call back function is: PrintOffDocuments. The PrintOffDocuments is the call back function because we want the secretary to do that, only after DropOffMail has run.
    • So I would "pass: PrintOffDocuments as an "argument" to the DropOffMail method. This is an important point.

    That's all it is. Nothing more. I hope that cleared it up for you - and if not, post a comment and I'll do my best to clarify.

提交回复
热议问题