In Javascript, functions are first-class objects, which means that functions can be used like objects according to first-class management. Since functions are actually objects: they can be "stored" in variables, passed as function parameters, created within functions, and returned from functions.
#Because functions are first-class objects, we can use callback functions in Javascript. In the following article, we will learn all about callback functions. Callback functions are probably the most used functional programming technique in Javascript. Although they literally look like a small piece of Javascript or jQuery code, they are still a mystery to many developers. After reading this article you can understand how to use callback functions.
The callback function is a concept derived from a programming paradigm called functional programming. Simply put, functional programming is about using functions as variables. Functional programming was - and still is - not widely used - it used to be seen as a secret technique reserved for specially trained, master-level programmers.
Fortunately, the technique of function programming has now been fully explained so that ordinary people like me and you can use it easily. One of the main tricks in functional programming is callback functions. In the following content, you will find that implementing a callback function is actually as simple as passing parameters to a normal function. This technique is so simple that I often wonder why it is often included in chapters on advanced Javascript techniques.
Explain what a callback or higher-order function is
A callback function, also known as a higher-order function, is a function that is passed as a parameter to another function ( Here we call the other function "otherFunction") function, the callback function is called in otherFunction. A callback function is essentially a programming pattern (a solution created for a common problem), so using callback functions is also called the callback pattern.
Here is a simple and common example of using callback functions in jQuery:
/注意到click方法中是一个函数而不是一个变量 //它就是回调函数 $("#btn_1").click(function() { alert("Btn 1 Clicked"); });
As you can see in the previous example, we passed a function as a parameter to the click method. The click method will call (or execute) the function we passed to it. This is a typical use of callback functions in Javascript, and it is widely used in jQuery.
Here is another example of a typical callback function in Javascript:
var friends = ["Mike", "Stacy", "Andy", "Rick"]; friends.forEach(function (eachName, index){ console.log(index + 1 + ". " + eachName); // 1. Mike, 2. Stacy, 3. Andy, 4. Rick });
Once again, notice that we passed an anonymous function (function without a name) as a parameter to the forEach method.
So far, we have passed anonymous functions as arguments to another function or method. Before we look at more practical examples and write our own callback functions, let's understand how callback functions work.
The above is the detailed content of What is js callback function. For more information, please follow other related articles on the PHP Chinese website!