Home > Web Front-end > JS Tutorial > Detailed explanation of the use of recursive functions in JavaScript

Detailed explanation of the use of recursive functions in JavaScript

黄舟
Release: 2017-11-18 10:50:44
Original
2294 people have browsed it

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); 
} 
}
Copy after login

The following code can cause an error:

var anotherFact = fact; 
fact = null; 
alert(antherFact(4)); //出错
Copy after login

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.
Copy after login

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); 
} 
}
Copy after login

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;
Copy after login

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); 
} 
}
Copy after login

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:

Analysis and explanation of recursive functions in JavaScript


Recursive function in JS


Refined understanding of recursive functions in JavaScript and sharing of sample code

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!

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