Understanding callback Functions in JavaScript (callback) _javascript tips

Source: Internet
Author: User
Tags function definition what callback

Recently looking at Express, eyeful look, everywhere is using function as a parameter of the callback function. If this concept is not understood, Nodejs, express code will be a mess. Like what:

Copy Code code as follows:

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 piece of code understand? Let's take a look at the concept of callback functions first.
First of all, in JS, the function is also an object, you can assign values to variables, can be placed as parameters in the function of the list of parameters. Like what:
Copy Code code as follows:

var dosomething = function (a,b)
{
return a + B;
}

The meaning of this code is to define an anonymous function, which is no different than a normal function, except that it has no name. The anonymous function is then assigned to the variable dosomething. Next we call:
Copy Code code as follows:

Console.log (dosomething (2,3));

This will output 5.

The callback function, which is placed in the argument 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:

Copy Code code 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);

Define the doit function first, with a parameter callback. This callback is the callback function, the name can be arbitrarily taken. Look at the function body, first define three variable a,b,c. Then call the callback function. Finally, a value is returned.

The doit function is called below. It should be noted that the definition of doit just now, callback did not define, so just did not know what callback is for. It's really good to understand that we usually define a function, the argument also just gives a name, such as a, that uses a in the body of the function, but the whole process does not know what a is, and specifies the specific value of a, such as 2, when the function is called. Back to the time, when calling doit, We need to specify what kind of thing callback is. As you can see, this function completes a sum function.

The preceding code is executed in the following procedure:

Call the doit function, the parameter is an anonymous function, enter the doit function body, first define the A,b,c, then execute the anonymous function, the parameter is A,b,c, and 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 it is not given here. These two examples in contrast, you can immediately understand.

In the use of Nodejs, Express, it is not possible for each method or function we have to find its function definition to see. So just know what parameters are passed to callback in that definition. Then, when calling the method or function, we define the anonymous function in the parameter to do some function.

over!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.