A little understanding of JS callback function

Source: Internet
Author: User

Previously written asynchronous JS is a pure translation, and now just work for 2 months, want to talk about my own JS callback function of a little understanding, welcome the desire for everyone to correct and exchange.

The callback function from the formal view is the function B as a parameter to function A, in a function body called function b

1 function A (b) {2    B (); 3 }4function  B () {5    ... 6 }

What is the point of doing this?

1. Control the execution process, function B only executes after function a executes

2. Depending on the requirements, there can be a variety of function B

3. Most importantly, it is possible to pass the data obtained through function A to function b

 1  function   A (callback) { 2   ...   3  var  record; //  After a series of complicated operations, I got the data record  4   callback (record);  5   6  function   b (data) { 7  ... // data to dispose of  8   9  A (b); 

We are interested in the record in function A and want to do a variety of operations on the record elsewhere in the program, then we can pass in the corresponding callback function. Of course, one of the obvious drawbacks of this approach (my current understanding) is that if we are interested in record_2, then we are not flexible enough.

A little understanding of JS callback function

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.