What is a callback function?

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

What is a callback function?

21条回答
  •  暗喜
    暗喜 (楼主)
    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.

提交回复
热议问题