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

Points to note when using recursive functions in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:48:071703browse

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>

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.

Statement:
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