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>
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>
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!