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

javascript中递归函数用法注意点_基础知识

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

不做详细文字说明了,直接写代码,很明了。

<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

上面的代码 在执行时很容易出现问题,执行我们介绍一个方法,arguments.callee,是一个指向正在执行的函数的指针,使用指针代替函数名使得执行以上代码时不容易出错!

以上代码就是javascript中递归函数用法注意点,希望对大家有所帮助。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!