callback function (callback) in JavaScript (reproduced)

Source: Internet
Author: User
Tags what callback

The code is as follows: App.use (function (req, res, next) {var err = new Error (' not Found ');     Err.status = 404; Next (ERR); }); App is object, use is method, method parameter is aanonymous functions with parameters, 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. For example: The code is as follows: var dosomething = function (A, a, a, b) {return a + C;} 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: The code is as follows: Console.log (dosomething (2,3)); This will output 5. callback function, which isIn a parameter list of another function (such as parent), passed as a parameter to the parent, and then executed at a location in the body of the parent function。 To be abstract, look at the example: the code 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); 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 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 well understood, when we usually define the function,
The parameter also gives a name, such as a, to use a in the body of the function, but the whole process does not know what a is, 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 doit function body, first define A,B,C, then execute the anonymous function just now, the argument 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 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 the method or function is called,
In the parameters we define our own anonymous function to accomplish certain functions.

callback function (callback) in JavaScript (reproduced)

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.