Home > Web Front-end > JS Tutorial > Conceptual understanding and usage analysis of javascript callback functions

Conceptual understanding and usage analysis of javascript callback functions

黄舟
Release: 2017-05-28 10:24:12
Original
1502 people have browsed it

This article mainly introduces the conceptual understanding and usage of javascriptcallback function, and analyzes the function, principle, usage and related aspects of javascript callback function in the form of specific examplesNotes, friends in need can refer to the following

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

jsThe code will be executed from the top to the next line , but sometimes we need to wait until one operation is completed before proceeding to the next operation. In this case, we need to use a callback function.

2. Explanation of callback function

Because the function is actually a object, it can be stored in a variable ", pass parameters to another function, create it inside the function, and 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 It returns, which has always been regarded as a difficult technology by "professional programmers"

The English explanation of the callback function is:

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

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

Function A has a parameter function B, and function B will be called and executed after function A is executed.

3. Callback. How to use the 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, then calls the callback function b, and finally Return the return value of function a

.

The above is the detailed content of Conceptual understanding and usage analysis 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