Home > Web Front-end > JS Tutorial > Points to note when using recursive functions in JavaScript_Basic knowledge

Points to note when using recursive functions in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 15:48:07
Original
1718 people have browsed it

I won’t give a detailed text explanation, just write the code directly, it’s very clear.

<script>
 function sum(num){
 if(num<=1){
return 1;
}else{
return num*sum(num-1);
//return num*arguments.callee(num-1); //指针
//return 2;
}
}
var sum1=sum;
 alert(sum1(2));
</script>
Copy after login

The above code is prone to problems when executed. We introduce a method for execution, arguments.callee, which is a pointer to the function being executed. Using pointers instead of function names makes it less likely to go wrong when executing the above code!

The above code is a note on the usage of recursive functions in JavaScript. I hope it will be helpful to everyone.

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