Understand the callback functions in JavaScript

Source: Internet
Author: User

To understand the callback function, you first know that in JavaScript, the function is also an object, it can be assigned to a variable, or it can be passed as a parameter to another function. Like what:

var add=function(b) {   Console.log (a+b);}; // 3Add (+);

A variable add is defined here, and an anonymous function is assigned to the variable;

The next demonstration is to pass the function as an argument to another function, in other words: Place one function in the argument list of another function (such as parent), pass it as a parameter to the parent, and execute it somewhere in the parent function body. In fact, this is the callback function. Look at a little chestnut:

 var  fun=function   (callback) { var  a=1,b=2,c=3;     var  s=callback (A,b,c);  return   S;}; 
// call fun var t=fun (function (x, y, z) { Span style= "color: #0000ff;" >return (X+y+z);}); // 6 console.log (t);

The first is to define the function Fun,fun there is a parameter callback (name can be arbitrarily), before calling the fun function, do not know what the callback is, the call is passed to it is a functional function (x, y, z) {return (x+y+z);} As a parameter, the value of S is the value that is added to the returned three number. This is a simple procedure to use a callback function.

Understand the callback functions in JavaScript

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.