The following is from: http://www.jb51.net/article/54641.htm
Recently, looking at Express, eyeful, everywhere is the use of callback functions as parameters of the function. If this concept is not understood, Nodejs, express code will see a mess. Like what:
App.use (function (req, res, next) {
var err = new Error (' not Found ');
Err.status = 404;
Next (ERR);
});
The app is an object, use is a method, the parameter of the method is an anonymous function with parameters, and the function body is given directly at the back. How does this code understand? Let's begin by understanding the concept of callback functions.
First of all, in JS, the function is also an object, you can assign a value to the variable, can be placed as a parameter in the function's argument list. Like what:
var dosomething = function (A, B)
{
return a + B;
}
The meaning of this code is to define an anonymous function, which, in addition to having no name, is nothing like a normal function. The anonymous function is then assigned a value to the variable dosomething. Next we call:
Console.log (dosomething (2,3));
This will output 5.
The callback function, which is placed in a parameter list of another function (such as parent), is passed as a parameter to the parent and then executed at a location in the parent function body. To be abstract, look at an example:
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);
Define the doit function first and have a parameter callback. This callback is the callback function, the name can be arbitrarily taken. Look at the function body, first define three variables A,b,c. Then call the callback function. Finally, a value is returned.
The doit function is called below. Note that the definition of doit just now, callback is not defined, so just did not know what callback is for. This is actually very good understanding, we usually define the function, the parameters are only given a name, such as a, in the function body to use a, but the whole process is not aware of what is a, only when the function is called to specify the specific value of a, such as 2. Turn back, when calling doit, We need to specify what the callback is. As you can see, this function completes a sum function.
The execution of the above code is:
Call the doit function, the parameter is an anonymous function, enter the function body of doit, define A,B,C first, then execute the anonymous function just now, the parameter is a,b,c, return a T, and finally return a t+10 to D.
Back to the original example, App.use (...) is a function call. We can imagine that a use method must have been defined before, but not given here. A comparison of these two examples can be understood immediately.
In the use of Nodejs, Express, it is not possible for each method or function we have to find its function definition to look at. So just know what parameters are passed to the callback in that definition. Then, when invoking a method or function, we define the anonymous function in the parameter to accomplish some function.
--------------------------------------------------------------------------------------------------
The following is from: http://www.cnblogs.com/xcsn/archive/2013/01/03/2843218.html
When using jquery, use the concept of callback (), the callback function. and a lot.
Like what:
$.ajax ({
URL: "Test.json",
Type: "GET",
Data: {username:$ ("#username"). Val ()},
DataType: "JSON",
Beforsend:function () {
Disable buttons to prevent duplicate submissions
$ ("#submit"). attr ({disabled: "disabled"});
},
Complete:function (msg) {
callback function called after the request completes (called when the request succeeds or fails)
} ,
Error:function (msg) {
function that is called when a request fails
} ,
Sucess:function (msg) {
callback function called after the request succeeds
}
});
-------------------------------------------------------------------------
callback function everyone will use, but jquery encapsulation, you can not let everyone understand the real use of the callback function.
The JS Api explains this: a callback is a function, which is passed as an argument to another function and are executed after its parent F Unction has completed. Of course we can actually try out the magic of the callback function in JS. If you call it directly in function A, the callback function is limited to death. But using the function to do the argument has the following advantages: When you have a (b) function B is a callback function, and you can also a (c) this time, function c is a callback function. If you write function a () {...; b ();} The flexibility of the variable is lost. The following is the code:As
you can see, I wrote jquery's Ajax functions in a (callback), B (), C () method, and note that this is the function of Ajax. There is a difference, I call the built-in function separately, and my own defined function.
The difference is here, because jquery and JS use the same callback, so in this case, jquery's built-in function will be called.
but the function that you define is not recognized, even if it is not called.
---------------------------------------------------------------------------------------------------------
Understanding the callback function in JavaScript (callback)