Home > Web Front-end > JS Tutorial > Detailed explanation of js callback

Detailed explanation of js callback

小云云
Release: 2018-03-30 16:09:49
Original
1626 people have browsed it

This article mainly introduces you to the detailed explanation of js callbacks. When I was using js, I just came across a callback, and I almost saved myself from it, so I could get out again. Now I have many ideas.

Let’s start with a simple code:

<script>
	var rows = [{name:"123"},{name:"456"}];
	var each = function(fun){
		for(var i = 0;i < rows.length;i++){
			fun(rows[i]);
			}
		}
	var fun = function(param){
		alert(param.name);
	}
	each(fun);
</script>
Copy after login

I created an array of rows with two objects in it,

Then I wrote Each method, and a fun method was written, and then I used each to call the fun method, using the result of each as the parameter of fun.

It’s so simple and beautiful, but what we see may not be so obvious, and then change it:

<script>
	var rows = [{name:"123"},{name:"456"}];
	var each = (function(fun){
		for(var i = 0;i < rows.length;i++){
			fun(rows[i]);
			}
		})
	var fun = function(param){
		alert(param.name);
	}
	//eachfun;
</script>
Copy after login

In fact, the kind of good-looking and the kind of good-looking depends on the individual, I think the first one is because there are each (fun), so that it is easy to distinguish. I will give a simple understanding here, not very in-depth, and then attach a link to explain it in more detail:

Related recommendations:

Introduction to several PHP callback functions

Completely master js callback functions

Detailed explanation of javscript callback functions

The above is the detailed content of Detailed explanation of js callback. 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