Home > Web Front-end > JS Tutorial > Introduction to the use of recursive functions in js

Introduction to the use of recursive functions in js

黄舟
Release: 2016-12-12 14:41:12
Original
1261 people have browsed it

Let’s try a factorial within 10:

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>js中递归函数的使用</title> 
<script type="text/javascript"> 
function f(num){ 
if(num<1){ 
return 1; 
}else{ 
return f(num-1)*num; 
} 
} 
</script> 
</head> 
<body> 
<script type="text/javascript"> 
alert("10!的结果为:"+f(10)); 
</script> 
</body> 
</html>
Copy after login

That’s all for calling recursive functions

The insurance method when js recursive functions call themselves.
From js advanced programming
A typical factorial recursive function:

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)); //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:

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 ordinary recursion in JS

Recursive functions are formed when a function calls itself by name, as shown below:

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 anotherFactory = factorial;

anotherFactorial(4); //Output 24
factorial = null;
anotherFacttorial (4); //TypeError: Property 'factorial' of object [object Window] is not a Test under function chrome
The reason is that the function name we defined is actually a pointer to the function, and anotherFacttorial is defined at this time It also points to that function, so calling anotherFactory (4) can successfully output 24
At this time factorial = null; Then the reference to the execution definition function is anotherFactorial, and the above error message will be displayed when calling anotherFactorial(4).
At this time, arguments.callee can be used to replace factorial in the function definition.
The definition of the function becomes:

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

Then using the above 4 lines of test code, the last line of test code can also successfully output 24.

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