This is explained in the JavaScript API: A callback is a function which is passed as an argument to another function and are executed after I The TS parent function has completed. (A callback is a function that is passed as a parameter to another function, and its parent function is executed after completion.) )
Reason for using a callback function: You can separate the caller from the callee. The caller does not care who the callee is, all it needs to know, but there is a called function that has a certain prototype, some restrictions (such as a return value of int).
I. Working with arrays using functionsto find the average of an array
Method One (normal mode):
var data = [1,1,3,5,5];var sum = 0;for (var i=0; i<data.length; i++) {sum + = Data[i];} Console.log (sum/data.length);
Method two (function type):
var data = [1,1,3,5,5];var sum = function (x, y) {return x+y;}; Console.log (Data.reduce (sum)/data.length);
PS: Invokes the specified callback function for all elements in the array, and the return value is the cumulative result obtained by the last call to the callback function.
Array1.reduce (callbackfn[, InitialValue])
If InitialValue is provided, the reduce method invokes the CALLBACKFN function (in ascending index order) once for each element in the array. If InitialValue is not provided, the reduce method invokes the CALLBACKFN function on each element starting with the second element.
The return value of the callback function is provided as the Previousvalue parameter the next time the callback function is called. The return value obtained from the last call to the callback function is the return value of the reduce method.
var data = [n/a];
Data.reduce (function (x, y) {return x+y},2); 8
Data.reduce (function (x, y) {return x+y}); 6
second, callback functionExample: Data source for a student score, when score<=0 is processed by the underlying, when the score>0 is processed by a high level.
/* callback function */function F (score,callback1,callback2) {if (score <= 0) {console.log ("Call the underlying handler function")/* * Use the calling function to pass parameters */ Callback1.call (This,score);} Else{console.log ("Call a high-level handler");/* Use the Apply function to pass parameters */callback2.apply (This,[score]);}}
/* Underlying function */function subprocess (score) {score = = = 0? console.log ("The student did not take the exam!") "): Console.log (" Input Error! ");} /* High function */function supprocess (score) {if (score >=) {Console.log ("the student has excellent grades!) ");} else if (score >=) {Console.log ("The Student scores well! ");} else if (score >=) {Console.log ("the student has passed the grade!") ");} Else{console.log ("The student did not pass the grade!") ");}}
/* Anonymous function */var score = 99;f (Score,function () {score = = = 0? console.log ("The student did not take the exam!") "): Console.log (" Input Error! },function () {if (score >=) {Console.log ("the student has excellent grades! ");} else if (score >=) {Console.log ("The Student scores well! ");} else if (score >=) {Console.log ("the student has passed the grade!") ");} Else{console.log ("The student did not pass the grade!") ");}})
third, higher order functionthe so-called high-order function is the function of an operation function that receives one or more functions as arguments and returns a new function.
/* High-order function, return F of the return value of the logical non-*/function not (f) {return function () {var result = f.apply (this,arguments); return!result;};} /* Determine if x is an even function */var even = function (x) {return x% 2 = = = 0;}; var odd = not (even);//A new function, the things done and even () opposite [1,1,3,5,5].every (odd); True, each element is an odd number
JavaScript callback function