Home > Web Front-end > JS Tutorial > body text

Analyze the conceptual understanding and usage of javascript callback functions

怪我咯
Release: 2017-05-29 09:51:24
Original
1331 people have browsed it

This article mainly introduces the conceptual understanding and usage of javascript callback functions. It analyzes the functions, principles, usage methods and related precautions of javascript callback functions based on specific examples. Friends in need can refer to it

The examples in this article describe the conceptual understanding and usage of javascript callback functions. Share it with everyone for your reference, the details are as follows:

1. The role of the callback function

js code will be executed from the top to the next line, but sometimes we need Wait until one operation is completed before proceeding with the next operation. At this time, you need to use the callback function.

2. Explanation of callback function

Because a function is actually an object, it can be stored in a variable and passed to another function through parameters. In the function Created internally, return the result value from the function", because the function is a built-in object, we can pass it as a parameter to another function, execute it in the function, and even return it after execution, it has always been used by "professional programmers" Seen as a difficult technology, the English explanation of

is:

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

Translation is: the callback function is a function passed as a variable to another function, which is executed after the main function is executed.

function A. There is a parameter function B. Function B will be called and executed after function A is completed.

3. How to use the callback function

The code is as follows:

function a(callbackFunction){
  alert("这是parent函数a");
  var m =1;
  var n=3;
 return callbackFunction(m,n);
}
function b(m,n){
  alert("这是回调函数B");
  return m+n;
}
$(function(){
  var result = a(b);
  alert("result = "+ result);
});
Copy after login

The execution sequence is:

这是parent函数a
这是回调函数B
result = 4
Copy after login

The function first executes the theme function a, and then calls Callback function b, and finally return the return value of function a

.

The above is the detailed content of Analyze the conceptual understanding and usage of javascript callback functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template