


Detailed analysis of javascript callback functions (graphic tutorial)
This article mainly introduces the relevant knowledge points of javascript callback functions and analysis of usage code examples. Friends in need can learn from it.
In an era when high-level languages are emerging in endlessly, each language claims to have the proud saying that everything is an object, but js, as a scripting language, is very different from traditional object-oriented languages such as Java. In addition to the weird inheritance system of js, one of the most fascinating features is the callback function. Of course, many people criticize it. The author believes that the callback function and asynchronous are the two most prominent features of the js language. Of course, just like all the advantages You need to meet your own needs. There is no silver bullet in this world. For example, using a large number of callback functions will make your code redundant and messy, affecting the visual and thinking experience of coders.
This article is my own review of learning callback functions My experience is inevitably imperfect or even wrong. Forgive my ignorance. I am willing to learn with an open mind and improve myself.
The most unique thing about JavaScript is that functions are first-class citizens. I prefer to call js everything. A high-level language for variables. Functions are also variables that can become parameters of other functions. We like to call them callback functions in js. In addition, anonymous functions in js can be used as function parameters, making callback functions easy to write.
ps. The callback function is similar to the function pointer in the C language---pointer pointer!!! Important things must be said three times
Let’s first look at the definition of the callback function
In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time
The callback function is a piece of executable code Code segment, which is passed as a parameter to other code, its function is to facilitate calling this piece of code (callback function) when needed.
Callback function
function addSqua(num1, num2, callback){ var sum = num1 + num2; return callback(sum); } function squa(num){ return num*num; } let num = addSqua(1, 2, squa); console.log(num); //=>9
Anonymous callback function
Pass the anonymous function directly to the function as a function parameter, which is what we generally use in programming
function addSqua(num1, num2, callback){ var sum = num1 + num2; return callback(sum); } let num = addSqua(1, 2, function squa(num){ return num*num; }); console.log(num); //=9
Imitate the every method in the array
Add a method similar to every in Array.prototype
Array.prototype.myEvery = function (callback) { if (typeof callback === 'function') { for (let i = 0; i < this.length; i++) { if (callback(this[i])) { return false; } } return true; } return true; } let op = [3, 6, 'every', 9,'each']; op.myEvery(function (value) { console.log(value); }) // 3 // 6 // 'every' // 9 // 'each'
Callback function features
will not be executed immediately. As given in the definition, the callback function as a code fragment can be executed under its specific conditions as needed. When the callback function is passed as a parameter to a function , only the function definition is passed and will not be executed immediately.
#this, ES6 introduced arrow functions. The introduction of arrow functions requires special attention when writing callback functions. this is the context object pointing to the function where it is located. If you use arrow functions in ES6 You need to pay attention to the use of this. There is no contextual relationship with this in the arrow function. If you are interested, you can check out ES6 Ruan Yifeng, who once again presents the advantages and usage scenarios of the portal
callback function It is a closure
The callback function is a simple use of closure, which means that it can access the variables defined in its outer layer.
The beauty of callback functions
DRY to avoid duplication of code.
Can abstract common logic.
Business logic separation (it's so beautiful ^-^ ^-^)
Improve code maintainability and readability.
Enhance code readability.
Separate dedicated functions.
The great thing about js callback function is that other traditional languages do not have it. It can realize the separation of business logic, which is equivalent to exposing an interface to the outside world. This is very similar to the separation of front and back ends. API interface design concept in the architecture
There is no 'silver bullet' in this world, and there is no one solution that can solve everything. Since js is born for asynchronous, callback functions are used most often and are also the most suitable. The problem is asynchronous programming. However, extensive use will make the programmer's code redundant, with a large number of unreadable lines, and the experience is extremely poor. It is simply a joyful thing. People have never stopped pursuing their own comfort.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to implement sliding puzzle verification code in JS
vuejs中v -Detailed explanation of using if and v-show
Detailed explanation of the steps to implement red-black tree using JS
The above is the detailed content of Detailed analysis of javascript callback functions (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Title: Analysis of the reasons and solutions for why the secondary directory of DreamWeaver CMS cannot be opened. Dreamweaver CMS (DedeCMS) is a powerful open source content management system that is widely used in the construction of various websites. However, sometimes during the process of building a website, you may encounter a situation where the secondary directory cannot be opened, which brings trouble to the normal operation of the website. In this article, we will analyze the possible reasons why the secondary directory cannot be opened and provide specific code examples to solve this problem. 1. Possible cause analysis: Pseudo-static rule configuration problem: during use

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Title: Is Tencent’s main programming language Go: An in-depth analysis. As China’s leading technology company, Tencent has always attracted much attention in its choice of programming languages. In recent years, some people believe that Tencent mainly adopts Go as its main programming language. This article will conduct an in-depth analysis of whether Tencent's main programming language is Go, and give specific code examples to support this view. 1. Application of Go language in Tencent Go is an open source programming language developed by Google. Its efficiency, concurrency and simplicity are loved by many developers.

Analysis of the advantages and limitations of static positioning technology With the development of modern technology, positioning technology has become an indispensable part of our lives. As one of them, static positioning technology has its unique advantages and limitations. This article will conduct an in-depth analysis of static positioning technology to better understand its current application status and future development trends. First, let’s take a look at the advantages of static positioning technology. Static positioning technology achieves the determination of position information by observing, measuring and calculating the object to be positioned. Compared with other positioning technologies,
