JavaScript callback function, javascript
The JavaScript API explains as follows: A callback is a function that is passed as an argument to another function and is executed after its parent function has completed. (a callback is a function that is passed as a parameter to another function. The callback is executed after the parent function is completed .)
Reason for using the callback function: you can separate callers from callers. The caller does not care who is called. All they need to know is that there is a called function with a specific prototype and certain restrictions (for example, the returned value is int.
I. Use functions to process arrays and calculate the average value of Arrays
Method 1 (conventional ):
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 2 (function ):
var data = [1,1,3,5,5];var sum = function(x,y){return x+y;};console.log(data.reduce(sum)/data.length);
PS: Call the specified callback function for all elements in the array. The returned value is the cumulative result obtained by the last callback function call.
Array1.reduce (callbackfn [, initialValue])
If initialValue is provided, the reduce method calls the callbackfn function (index order in ascending order) for each element in the array ). If initialValue is not provided, the reduce method calls the callbackfn function for each element starting with the second element.
The Return Value of the callback function is provided as the previusvalue parameter when the callback function is called the next time. The return value obtained by the last callback function is the return value of the reduce method.
Var data = [1, 2, 3];
Data. reduce (function (x, y) {return x + y}, 2); // 8
Data. reduce (function (x, y) {return x + y}); // 6
Ii. Callback Function example: if the data source is a score of a student, score <= 0 is processed by the underlying layer, and score> 0 is processed by the high-level layer.
/* Callback function */function f (score, callback1, callback2) {if (score <= 0) {console. log ("call underlying processing functions")/* use the call function to PASS Parameters */callback1.call (this, score);} else {console. log ("Call high-level processing functions");/* use the apply function to PASS Parameters */callback2.apply (this, [score]);}
/* Underlying function */function subprocess (score) {score = 0? Console. log ("this student has not taken the test! "): Console. log (" input error! ");}/* High-level function */function supprocess (score) {if (score> = 90) {console. log (" this student scored well! ");} Else if (score> = 80) {console. log (" this student scored well! ");} Else if (score> = 60) {console. log (" this student passed the score! ");} Else {console. log (" this student failed! ");}}
/* Anonymous function */var score = 99; f (score, function () {score = 0? Console. log ("this student has not taken the test! "): Console. log (" input error! ") ;}, Function () {if (score> = 90) {console. log (" this student scored well! ");} Else if (score> = 80) {console. log (" this student scored well! ");} Else if (score> = 60) {console. log (" this student passed the score! ");} Else {console. log (" this student failed! ");}})
3. High-order functions are called high-order functions, which receive one or more functions as parameters and return a new function.
/* High-order function. The logic returned by f is not */function not (f) {return function () {var result = f. apply (this, arguments); return! Result ;};}/* determine whether x is an even number */var even = function (x) {return x % 2 = 0 ;}; var odd = not (even); // a new function that does the opposite of even () [, 5]. every (odd); // true, each element is an odd number