How to explain callbacks in plain english? How are they different from calling one function from another function?

后端 未结 30 1949
时光说笑
时光说笑 2020-11-22 11:13

How to explain callbacks in plain English? How are they different from calling one function from another function taking some context from the calling function? How can thei

30条回答
  •  深忆病人
    2020-11-22 11:24

    Callbacks allows you to insert your own code into another block of code to be executed at another time, that modifies or adds to the behavior of that other block of code to suit your needs. You gain flexibility and customizability while being able to have more maintainable code.

    Less hardcode = easier to maintain and change = less time = more business value = awesomeness.

    For example, in javascript, using Underscore.js, you could find all even elements in an array like this:

    var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
    => [2, 4, 6]
    

    Example courtesy of Underscore.js: http://documentcloud.github.com/underscore/#filter

提交回复
热议问题