Simple practical analysis of callbacks and recursive functions in Javascript
1 callback function
The so-called callback function is simple to understand is to pass a function as a parameter to other functions for its use . (Only in JS , because the concept of pointers is in other languages).
To give a simple example, when we are counting the bills, we need to sort out the materials, and then we want calculators, calculators, and we think of it as a function that can be calculated. The statistical bill is another function, and when the bill is counted it will need the support of the calculator function, which is actually a simple callback. can follow this understanding.
Down I borrowed an example from the Internet:
First define a function fun1function fun1 (num1,num2) {alert (num1+num2);} Define a function Fun2function fun2 (num1,num2,fun) {fun (num1,num2); }//Last Call Function fun2fun2 (3,5,FUN1);
Result is8procedure: When callingfun2function, the time passed in3 5 fun1three parameters are triggered when they are passed infun1the function to Funin thefun2run in,NUM1and thenum2for the incoming3 5the final output of their and8
There is also an anonymous callback in the callback
Function fun2 (Num1,num2,fun) {fun (num1,num2);} Fun2 (3,5,function (num1,num2) {alert (num1+num2);});
In simple terms, a function that was previously defined earlier is called when it is called.
Another way to do this is:
(function fun2 (num1,num2,fun) {fun (num1,num2);}) (3,5,function (num1,num2) {alert (num1+num2); });
Parentheses are meant to be executed directly.
Recursive functions:
The recursive function is a sentence: call yourself directly or indirectly within a function.
Understanding recursive functions requires a certain understanding of the concept of a stack.
Stack: There is a proverb called the brick bricks from behind. The first thing to put down is finally taken out.
Imagine a bamboo tube that is not large in diameter and closed at one end of the opening. There are several small balls with numbered balls, the diameter of which is slightly smaller than the diameter of a bamboo tube. Now put different numbers of small balls into the inside of the bamboo tube, you can find a law: first put in the ball can only be taken out, on the contrary, after the ball can be put in the first to take out. So the " advanced out " is the characteristic of this structure.
A stack is a data structure that opens up a storage area in memory, where the data is stored in a sequential order (that is, push-in), and there is an address pointer that always points to the cell where the last data that is pressed into the stack The register that holds this address pointer is called a stack indicator. The unit to begin with is called the "bottom of the stack". Data A single deposit process called the stack 1 1 " popup pop "
A stack is the most commonly used data structure in a computer, such as a function call that is implemented in a stack on a computer.
The stack can be stored in an array, or it can be stored with a list of the lists that will be introduced later.
Here is just a simple understanding.
Down to understand the recursive function:
Function num (NUM1) {If (num1>1) {num (--NUM1);} document.write (NUM1)}num (4);
The result of the output is 1123  num 4>1 set up and execute its own num 3  --NUM  321 1 no direct press is formed, and the last stack takes a LIFO rule, so the last 1123
This is just a simple js callback and recursive introduction.
This article is from the "Running Bronco" blog, please be sure to keep this source http://chongge.blog.51cto.com/8894476/1752197
Simple practical analysis of callbacks and recursive functions in Javascript