Understanding and usage of callback functions in js, js callback Functions

Source: Internet
Author: User

Understanding and usage of callback functions in js, js callback Functions

Understanding and usage of callback functions in js

I. Functions of the callback function

The js Code goes down to the next line, but sometimes we need to wait until an operation ends before performing the next operation. At this time, we need to use the callback function.

Ii. Explanation of callback Functions

Because a function is actually an object, it can be stored in variables, passed to another function through parameters, created inside the function, and returned result values from the function ", because a function is a built-in object, we can pass it as a parameter to another function, execute it in the function, or even return it after execution, it has always been regarded as an obscure technology by "professional programmers.

The callback function is interpreted as follows:

A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.

The callback function is a function that is passed as a variable to another function. It is executed after the main function is executed.

Function A has function B. function B is called and executed after function A is executed.

Iii. Usage of callback Functions

The Code is as follows:

Function a (callbackFunction ){
Alert ("this is parent function ");
Var m = 1;
Var n = 3;
Return callbackFunction (m, n );
}
Function B (m, n ){
Alert ("this is callback function B ");
Return m + n;
}
$ (Function (){
Var result = a (B );
Alert ("result =" + result );
});

The execution sequence is as follows:

This is the parent function.

This is the callback function B.

Result = 4

The function first executes theme function a, then calls callback function B, and finally returns the return value of function.

 

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.