search
HomeWeb Front-endJS TutorialIntroduction to the use of recursive functions in js_javascript skills

Let’s try a factorial within 10:


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]



So much for calling recursive functions

The insurance method when js recursive functions call themselves.
From js advanced programming
A typical factorial recursive function: Copy code
The code is as follows:


function fact(num){
if (numreturn 1;
}else{
return num*fact(num-1);
}
}


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: Copy the code
The code is as follows:


function fact(num ){
if (numreturn 1;
}else{
return num*arguments.callee(num-1); //Changed here.
}
}
var anotherFact = fact;
fact = null;
alert(antherFact(4)); //The result is 24.


Improvements of ordinary recursion in JS

Recursive functions are formed when a function calls itself by name, as follows: Copy code
The code is as follows:


function factorial(num)
{
if(num{
return 1;
}
else
{
return num * factorial(num-1);
}
}


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); //Output 24
factorial = null;
anotherFactorial (4); //TypeError: Property 'factorial' of object [object Window] is not a function. Test under chrome
The reason is that the function name we defined 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 the reference of the execution definition function is anotherFactorial. Then 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: Copy code
The code is as follows:


function factorial(num)
{
if(num{
return 1;
}
else
{
return num * arguments.callee(num-1);
}
}


Then use the above 4 lines of test code, The last line of test code can also be successfully output 24. ---------------------------------------- -- The above content is excerpted from section 7.1 on page 144 of the 2nd edition of >
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
如何解决Python的代码中的函数嵌套过多错误?如何解决Python的代码中的函数嵌套过多错误?Jun 25, 2023 pm 12:35 PM

Python是一门非常强大的编程语言,很多程序员都选择Python作为主要的编程语言。但是,代码中过多的函数嵌套会导致程序难以维护和理解。本文将探讨如何解决Python的代码中的函数嵌套过多错误。函数嵌套浅谈函数嵌套是指在一个函数的主体中定义另外一个函数的过程。函数嵌套可以使程序的结构更加清晰,代码也更易于阅读和维护。但是,函数嵌套过多会导致代码结构过于复杂

C++ 递归函数的优化技巧有哪些?C++ 递归函数的优化技巧有哪些?Apr 17, 2024 pm 12:24 PM

为了优化递归函数的性能,可以采用以下技巧:使用尾递归:将递归调用放在函数末尾,避免递归开销。备忘录化:存储已计算的结果,避免重复计算。分治法:分解问题,递归解决子问题,提高效率。

C++ 递归函数在搜索算法中的应用?C++ 递归函数在搜索算法中的应用?Apr 17, 2024 pm 04:30 PM

递归函数在搜索算法中用于探索树状数据结构。深度优先搜索使用堆栈探索节点,而广度优先搜索使用队列按层遍历。在实际应用中,如查找文件中,递归函数可用于在指定目录中搜索给定文件。

C++ 递归函数的退出条件是什么?C++ 递归函数的退出条件是什么?Apr 17, 2024 am 11:33 AM

C++递归函数的退出条件包括:基线条件:检查函数是否达到可直接返回结果的状态,通常判断某个条件或参数值是否满足阈值。递归终止条件:替代或补充基线条件,确保函数在一定数量的递归调用后停止,通过跟踪递归深度或设置最大递归深度限制实现。

C++ 递归函数在排序算法中的应用?C++ 递归函数在排序算法中的应用?Apr 17, 2024 am 11:06 AM

C++中递归函数在排序算法中的应用通过递归函数实现的插入排序和归并排序算法,可以将复杂的问题分解为更小的子问题,并通过递归调用高效地解决。插入排序:通过逐个插入元素,将数组有序化。归并排序:分而治之,将数组拆分并递归排序子数组,最后将排序后的子数组合并。

C++ 递归函数的尾递归优化策略如何实现?C++ 递归函数的尾递归优化策略如何实现?Apr 17, 2024 pm 02:42 PM

尾递归优化策略通过将尾递归调用转换为循环,有效减少函数调用栈深度,防止栈溢出。优化策略包括:检测尾递归:检查函数中是否存在尾递归调用。将函数转换为循环:使用循环来代替尾递归调用,并维护栈保存中间状态。

如何使用Go语言递归函数实现阶乘?如何使用Go语言递归函数实现阶乘?Jul 31, 2023 pm 08:31 PM

如何使用Go语言递归函数实现阶乘?阶乘是数学中常见的一种计算方式,它将一个非负整数n乘以比它小的所有正整数,直到1。例如,5的阶乘可以表示为5!,计算方式为54321=120。在计算机编程中,我们经常使用递归函数来实现阶乘的计算。首先,我们需要了解递归函数的概念。递归函数是指在函数的定义中调用函数本身的过程。在解决问题时,递归函数会不断地

Python编程:递归与匿名函数及函数属性与文档字符串(函数补充)Python编程:递归与匿名函数及函数属性与文档字符串(函数补充)Apr 12, 2023 pm 11:22 PM

本文简单扼要地说,辅以代码进一步地加深理解。 递归函数当函数调用自身而生成最终结果时,这样的函数称为递归。有时递归函数非常有用,因为它们使编写代码变得更容易——使用递归范式编写一些算法非常容易,而其他算法则不是这样。没有不能以迭代方式重写的递归函数,换句话说,所有递归函数都可以通过循环迭代的方式实现,因此通常由程序员根据手头的情况选择最佳方法。递归函数主体通常有两个部分:一部分的返回值依赖于对自身的后续调用,另一部分的返回值不依赖于对自身的后续调用(称基本情况,或递归边界)。作为理解的参考示例,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),