Analysis of callback function

Callback

Definition (in js): Function A is passed as a parameter (function reference) to another function B, and this function B executes function A. Let us say that function A is called a callback function. If there is no name (function expression), it is called an anonymous callback function.

Array.prototype.myForeach = function(handler){
      for(var i=0;i<this.length;i++){
        var item = this[i];
        // 回调
        handler(item)
      }
    }

 //调用【你】
var arr = ["terry","larry","tom"]
var handler = function(item){
  // 规定item是每次遍历出来的元素
  console.log(item);
} ;
arr.myForeach( handler );

Wherein the handler is a function, it passes to the myForeach as arguments, and this function is called, (handler (item)) is set to the item of the parameter handler, the handler function is performed, to obtain the desired results.
In When processing asynchronously, you must use the callback function to get the result of one of the asynchronous operations.

Guess you like

Origin blog.csdn.net/weixin_49549509/article/details/107989091