What is a callback function?

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

What is a callback function?

相关标签:
21条回答
  • 2020-11-21 23:26

    This makes callbacks sound like return statements at the end of methods.

    I'm not sure that's what they are.

    I think Callbacks are actually a call to a function, as a consequence of another function being invoked and completing.

    I also think Callbacks are meant to address the originating invocation, in a kind of "hey! that thing you asked for? I've done it - just thought I would let you know - back over to you".

    0 讨论(0)
  • 2020-11-21 23:30

    A layman response would be that it is a function that is not called by you but rather by the user or by the browser after a certain event has happened or after some code has been processed.

    0 讨论(0)
  • 2020-11-21 23:33

    Call After would be a better name than the stupid name, callback. When or if condition gets met within a function, call another function, the Call After function, the one received as argument.

    Rather than hard-code an inner function within a function, one writes a function to accept an already-written Call After function as argument. The Call After might get called based on state changes detected by code in the function receiving the argument.

    0 讨论(0)
  • 2020-11-21 23:35

    The simple answer to this question is that a callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another, when that pointer is used to call the function it points to it is said that a call back is made

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 23:38

    I believe this "callback" jargon has been mistakenly used in a lot of places. My definition would be something like:

    A callback function is a function that you pass to someone and let them call it at some point of time.

    I think people just read the first sentence of the wiki definition:

    a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code.

    I've been working with lots of APIs, see various of bad examples. Many people tend to name a function pointer (a reference to executable code) or anonymous functions(a piece of executable code) "callback", if they are just functions why do you need another name for this?

    Actually only the second sentence in wiki definition reveals the differences between a callback function and a normal function:

    This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.

    so the difference is who you are going to pass the function and how your passed in function is going to be called. If you just define a function and pass it to another function and called it directly in that function body, don't call it a callback. The definition says your passed in function is gonna be called by "lower-level" function.

    I hope people can stop using this word in ambiguous context, it can't help people to understand better only worse.

    0 讨论(0)
提交回复
热议问题