Recently, I'm looking at express, and I've seen it everywhere, using callback functions as parameters. If this concept cannot be understood, the code of nodejs and express will be very confused. for example:
The code copy is as follows:
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app is an object, use is a method, and the parameter of the method is an anonymous function with parameters, and the function body is directly given later. How do you understand this code? Let's first understand the concept of callback functions.
First of all, we need to understand that in js, functions are also objects, which can be assigned to variables and can be placed as parameters in the function's parameter list. for example:
The code copy is as follows:
var doSomething = function(a,b)
{
return a + b;
}
This code means to define an anonymous function. Except for having no name, this anonymous function is no different from an ordinary function. Then assign the anonymous function to the variable doSomething. Next we call:
The code copy is as follows:
console.log(doSomething(2,3));
This outputs 5.
The callback function is placed in the parameter list of another function (such as parent), passed to the parent as a parameter, and then executed at a certain position in the parent function body. To put it in abstraction, look at the example:
The code copy is as follows:
// To illustrate the concept of callback
var doit = function(callback)
{
var a = 1,
b = 2,
c = 3;
var t = callback(a,b,c);
return t + 10;
};
var d = doit(function(x,y,z){
return (x+y+z);
});
console.log(d);
First define the doit function, and there is a parameter callback. This callback is the callback function, and the name can be taken arbitrarily. Looking at the function body, first define three variables a, b, and c. Then call the callback function. Finally, a value is returned.
The following is the doit function. It should be noted that when you defined doit just now, callback was not defined, so you didn't know what callback is for. This is actually easy to understand. When we define a function, the parameters only give a name, such as a, using a in the function body, but the whole process does not know what a is. Only when the function is called is specified, such as 2. Looking back, when calling doit, we need to specify what callback is. As you can see, this function completes a sum function.
The execution process of the above code is:
Call the doit function, the parameter is an anonymous function; enter the function body of doit, first define a, b, c, and then execute the anonymous function just now, the parameters are a, b, c, and return a t, and finally return a t+10 to d.
Going back to the original example, app.use(...) is a function call. We can imagine that a use method must have been defined before, but it is not given here. By comparing these two examples, you can understand them immediately.
When using nodejs and express, it is impossible for us to find the function definition of each method or function to take a look. So just know what parameters are passed to callback in that definition. Then when calling a method or function, we define anonymous functions in the parameters to complete certain functions.
Over!