Home > Web Front-end > JS Tutorial > body text

Introduction to the use of recursive functions in js_javascript skills

WBOY
Release: 2016-05-16 17:48:50
Original
1423 people have browsed it

Let’s try a factorial within 10:


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]



So much for calling recursive functions

The insurance method when js recursive functions call themselves.
From js advanced programming
A typical factorial recursive function: Copy code
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)); //Error

Because fact is no longer a function, an error occurred.
The problem can be solved with arguments.callee, which is a pointer to the function being executed.
The new function is: Copy the code
The code is as follows:


function fact(num ){
if (num<=1){
return 1;
}else{
return num*arguments.callee(num-1); //Changed here.
}
}
var anotherFact = fact;
fact = null;
alert(antherFact(4)); //The result is 24.


Improvements of ordinary recursion in JS

Recursive functions are formed when a function calls itself by name, as follows: Copy code
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); //Output 24
factorial = null;
anotherFactorial (4); //TypeError: Property 'factorial' of object [object Window] is not a function. Test under chrome
The reason is that the function name we defined 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 the reference of the execution definition function is anotherFactorial. Then 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: Copy code
The code is as follows:


function factorial(num)
{
if(num<=1)
{
return 1;
}
else
{
return num * arguments.callee(num-1);
}
}


Then use the above 4 lines of test code, The last line of test code can also be successfully output 24. ---------------------------------------- -- The above content is excerpted from section 7.1 on page 144 of the 2nd edition of <>
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template