about callback Functions
Pass the callback function callback as a parameter to the asynchronous execution function, and then trigger callback execution when the asynchronous execution function finishes executing.
Defines an asynchronous execution function, a callback function as a parameter function
A (callback) {
callback ();
Console.log (' I am an async execution function ');
}
Define the callback function
B () {
setTimeout (' Console.log (' I am a callback function ') ', ' n '); Mimic time-consuming operation
}
A (B);
I am the asynchronous execution function
//I am the callback function
The code above illustrates that an asynchronous execution function can execute its own code (asynchronous operation) without waiting for the callback function to finish .
Advantages: Simple, easy to understand and deploy
Cons: not conducive to code reading and maintenance, the various parts are highly coupled, each task can only specify a callback function. Array Method
Array.prototype.map ()
The map () method returns a new array in which each element is the result of a callback function.
var numbers = [1, 4, 9];
var roots = Numbers.map (math.sqrt); The value of the call
//roots in order is [1, 2, 3], the value of numbers is still [1, 4, 9]
Array.prototype.join ()
The join () method joins all elements of an array (or a class array object) into a string .
Array.join (separator) Separator optional. Specifies the delimiter to use. If this argument is omitted, a comma is used as the delimiter.
var array = [1,2,3,4,5];
var str = array.join ("");
str = "12345", array = [1,2,3,4,5]