Let's take a look at what a callback function is (callback)
The callback function is called when the function is invoked , and the name of the other function is passed in as a parameter and is called in the body of the function .
function study () { console.log ("studying ...");} function Play () { console.log ("playing ...");} function Sleep () { console.log ("sleeping ....");} function dowhat (funname) { Console ("i am"); Funname ();} Dowhat (study); // I'm Studying ... Dowhat (play); // I'm Playing ... Dowhat (sleep); // I'm Sleeping ...
With the parameter
function study (name) { console.log (name+ "" + "studying ...");} function play (name) { console.log (name+ "" + "playing ...");} function sleep (name) { console.log (name+ "" + "sleeping ...");} function dowhat (funname,name) { funname (name);} Dowhat (study,' xiaoming '); // Xiaoming Studying ... Dowhat (play, ' Xiaodong '); // Xiaodong playing ...
Can also take a lot of parameters, we write a calculation example
function Add (b) { console.log (A +b);} function reduce (a, a ) {console.log (a-b);} function Calc (funname,fn1,fn2) { funname (fn1,fn2);} Calc (add,5,4);//9
Calc (REDUCE,5,4);//1
This is the callback function (callback);
Hopefully it will help you understand the callback function (end
JavaScript pain Point Four callback function