Understanding callback functions in javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to understand the callback function (callback) in javascript. This article focuses on understanding the callback function concept. If you need it, refer to the latest introduction to express, the use of callback functions with functions as parameters is everywhere. If you cannot understand this concept, the code of nodejs and express will be messy. For example:

The Code is as follows:


App. use (function (req, res, next ){
Var err = new Error ('not Found ');
Err. status = 404;
Next (err );
});


The app is an object, the use is a method, and the method parameter is an anonymous function with parameters. The function body is provided directly later. How can this code be understood? Let's first understand the concept of callback functions.
First, you must understand that in js, a function is also an object and can be assigned to a variable. It can be used as a parameter in the parameter list of the function. For example:

The Code is as follows:


Var doSomething = function (a, B)
{
Return a + B;
}


This Code defines an anonymous function, which is similar to a common function except that it has no name. Assign an anonymous function to the variable doSomething. Next we call:

The Code is as follows:


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

This will output 5.

The callback function is placed in the parameter list of another function (such as parent), passed as a parameter to this parent, and then executed at a certain position in the parent function body. Abstract: Let's look at the example:

The Code is as follows:


// To initialize strate 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. There is a callback parameter. This callback is the callback function, and the name can be any one. Look at the function body. First, define three variables a, B, and c. Then call the callback function. Returns the last value.

The following code calls the doit function. It should be noted that the callback is not defined when doit is defined just now, so we do not know what the callback is. This is actually quite understandable. When we define a function, the parameter only gives a name, for example, a, using a in the function body, however, the whole process does not know what a is. The specific value of a is specified only when the function is called, for example, 2. when calling doit, We need to specify what the callback is. As you can see, this function completes the sum function.

The code execution process is as follows:

Call the doit function. The parameter is an anonymous function. Enter the doit function body, define a, B, and c, and then execute the anonymous function. The parameter is a, B, c, return a t, and then return a t + 10 to d.

Back to the original example, app. use (...) is a function call. We can imagine that we have defined a use method before, but it is not provided here. The comparison between the two examples can be understood immediately.

When using nodejs and express, we cannot find its function definition for every method or function. So you only need to know what parameters are passed to callback in that definition. Then, when calling methods or functions, we define anonymous functions in parameters to complete some functions.

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.