In our previous article, we analyzed the recursive function in JavaScript. I believe you have some understanding of this, so here are the recursive functions in JavaScript How to use it? Today I will give you a detailed introduction to the use of recursive functions in JavaScript!
The so-called recursive function is to call this function within the function body. Be careful when using recursive functions. If not handled properly, you will enter an infinite loop. Recursive functions can only be used in specific situations, such as the factorial problem
Let’s try a factorial within 10:
[Ctrl+A Select all Note: If you need to introduce external Js needs to be refreshed before it can be executed]
So much for the call of the recursive function
The insurance method when the js recursive function calls itself.
From js advanced programming
A typical factorial recursive function:
The code is as follows:
function fact(num){ if (num<=1){ return 1; }else{ return num*fact(num-1); } }
The following code can cause an error:
var anotherFact = fact; fact = null; alert(antherFact(4)); //出错
Due to fact is no longer a function, so an error occurs.
The problem can be solved with arguments.callee, which is a pointer to the function being executed.
The new function is:
The code is as follows:
function fact(num){ if (num<=1){ return 1; }else{ return num*arguments.callee(num-1); //此处更改了。 } } var anotherFact = fact; fact = null; alert(antherFact(4)); //结果为24.
Improvements of JS ordinary recursion
The recursive function is called in a function by name It is formed under its own circumstances, as follows:
The code is as follows:
function factorial(num) { if(num<=1) { return 1; } else { return num * factorial(num-1); } }
This is a classic factorial function. On the surface, there seems to be no problem, but the following code may cause it to go wrong.
var anotherFactorial = factorial; anotherFactorial(4); //输出 24 factorial = null;
anotherFactorial (4); //TypeError: Property 'factorial' of object [object Window] is not a function Test under chrome
The reason is that we define The function name is actually a pointer to the function. At this time, anotherFactorial is defined and also points to that function, so calling anotherFactorial (4) can successfully output 24
At this time, factorial = null; then execute define the function The reference of only leaves anotherFactorial, so the above error message will be displayed when calling anotherFactorial(4).
At this time, you can use arguments.callee to replace factorial in the function definition.
The definition of the function becomes:
The code is as follows:
function factorial(num) { if(num<=1) { return 1; } else { return num * arguments.callee(num-1); } }
Then in Using the above 4 lines of test code, the last line of test code can also successfully output 24.
Summary:
Through the details of the above article Introduction, I believe that friends have a better understanding of the use of recursive functions in JavaScript, I hope it will be helpful to your work!
Related recommendations:
The above is the detailed content of Detailed explanation of the use of recursive functions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!