In-depth analysis of callback functions in js (detailed explanation sharing)

奋力向前
Release: 2021-09-17 10:53:09
forward
2160 people have browsed it

In the previous article "An article explaining the this pointing problem in js (with code)", I introduced you to the this pointing problem in js. The following article will give you a detailed explanation of the callback function in js. Friends, take a look.

In-depth analysis of callback functions in js (detailed explanation sharing)

Detailed explanation of JS callback function

JS callback function

What is a callback function, official explanation : When the program is running, under normal circumstances, the application program will often call pre-prepared functions in the library through the API. However, some library functions require the application to pass it a function first, so that it can be called at the appropriate time to complete the target task. The function that is passed in and called later is called the callback function.

Usually pass a functionBinto another functionA, and call function A when needed.

To put it bluntly, callback is backtracking. First define the function body to be used, and then call this function after use. We usually passcallbackas a parameter into the one defined first. function. Let's first look at a piece ofjquerycode:

$("p").hide(1000,function(){ alert("The paragraph is now hidden"); });
Copy after login

The above jquery is a callback function. First, after executing thehideeffect, it then calls thefunctionCallback.

Let’s look at a piece of JS code again to see how the callback function is implemented:

function Buy(name,goods1,callback) { alert(name+' buy '+goods1); if(callback&&typeof(callback)==="function") callback(); } Buy('xiaoming','apple',function(){ alert("shopping finish"); });
Copy after login

A very simple code. I don’t know what I want to buy at the beginning. When I buy something, I immediately put the previous When the defined function is called, it is best to add judgment rules, because the premise is thatcallbackmust be a function, and the output result is:

xiaoming buy apple shopping finish
Copy after login

Closure and callback

Let’s look at a closure question. Click on the first and fourth nodes respectively. The execution result is:

var nodes = document.getElementsByTagName('button'); for (var i = 0; i < nodes.length; i++) { nodes[i].addEventListener('click', function() { console.log('You clicked element #' + i); }); }
Copy after login

Although the main test here is closure,addEventListeneris a closure, and the anonymous function is a callback function, i is the variable in the closure. When the callback ofaddEventListeneris executed, the loop has ended. At this time, the variable i is assigned the valuenode.length, andnode.lengthis the total number of nodes. So the result is

You clicked element # node.length
Copy after login

How to change it, change its return value to a function, since the value of the i variable will be released, then we will introducei, so that the value of each loop Just save them all in memory:

var nodes = document.getElementsByTagName('button'); for (var i = 0; i < nodes.length; i++) { (function(i) { nodes[i].addEventListener('click', function () { console.log('You clicked element #' + i); }) })(i); }
Copy after login

PHP callback function

Let’s briefly talk about how to implement the callback function in PHP (the result is 1, 2, 3, 4):

Copy after login

php performs callback processing for each incoming array element. Let’s take a look at PHP’s closure. Use theusekeyword to inherit variables outside the scope:

Copy after login

The return result is:

1,1
Copy after login

Official PHP usage method

class hello { function callback($a,$b) { echo "$a,$b"; } static function callback($c,$d){ echo "$c,$d"; } } //将类名作为参数 call_user_func(array('hello','callback'),"hello","world"); //输出 hello,world //将对象作为参数 call_user_func(array(new hello(),'callback'),"hello","world"); //将静态方法作为参数 call_user_func(hello::callback,"hello","world"); //输出 hello,world
Copy after login

In fact, in principle, JS and PHP are similar. They both define the function first and then call it when needed. .

Recommended learning:JS video tutorial

The above is the detailed content of In-depth analysis of callback functions in js (detailed explanation sharing). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:juejin.im
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
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!