newbie approach: what is a javascript callback function?

前端 未结 7 2154
自闭症患者
自闭症患者 2020-11-28 12:49

Is just a function that executes after one other function that calls it, finishes?

Please I know (almost) nothing about programming, and I find it quite hard to find

7条回答
  •  自闭症患者
    2020-11-28 13:31

    Imagine you have some complex piece of code and right in the middle of it, you must do "something". The problem: You have no idea what "something" might be since that depends on how the code is used. Callback to the rescue.

    Instead of having code, you just put a callback there. The user can supply her own piece of code and it will be executed just at the right time to do some critical thing. Example: jQuery.filter(callback).

    jQuery (which you should have a look at) will call callback for each element in a list. The callback function can then examine the element and return true if it matches some criteria.

    This way, we have the best of two worlds: The smart people at jQuery create the filter and you say what exactly to look for.

提交回复
热议问题