javascript 递归函数调用(recursive funciton call)
所谓的递归函数调用,就是自己调用自己的函数。
var timerHandler = null;
function a(){
console.log(123);
timerHandler = setTimeout(a, 1000) ;
}
a();
//clearTimeout(timerHandler);
-----------------------------------------------------------------------------------------
/* Count down to 0 recursively.
*/
var functionHolder = function (counter) {
console.log(counter);
if (counter > 0) {
functionHolder(counter-1);
}
}
With this, functionHolder(3);
would output 3
2
1
0
. Let's say I did the following:
var copyFunction = functionHolder;
copyFunction(3);
would output3
2
1
0
as above. If I then changedfunctionHolder
as follows:
functionHolder = function(whatever) {
console.log("Stop counting!");
}
Then functionHolder(3);
would give Stop counting!
, as expected.
copyFunction(3);
now gives 3
Stop counting!
as it refers to functionHolder
, not the function (which it itself points to). This could be desirable in some circumstances, but is there a way to write the function so that it calls itself rather than the variable that holds it?
That is, is it possible to change only the line functionHolder(counter-1);
so that going through all these steps still gives 3
2
1
0
when we call copyFunction(3);
? I tried this(counter-1);
but that gives me the error this is not a function
.
----------------------------------------------------------------------------------------------
Using Named Function Expressions:
You can give a function expression a name that is actually private and is only visible from inside of the function ifself:
var factorial = function myself (n) {
if (n <= 1) {
return 1;
}
return n * myself(n-1);
}
typeof myself === 'undefined'
Here myself
is visible only inside of the function itself.
You can use this private name to call the function recursively.
See 13. Function Definition
of the ECMAScript 5 spec:
The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression cannot be referenced from and does not affect the scope enclosing the FunctionExpression.
Please note that Internet Explorer up to version 8 doesn't behave correctly as the name is actually visible in the enclosing variable environment, and it references a duplicate of the actual function (see patrick dw's comment below).
javascript 递归函数调用(recursive funciton call)的更多相关文章
- JavaScript递归原理
JavaScript递归是除了闭包以外,函数的又一特色呢.很多开发新手都很难理解递归的原理,我在此总结出自己对递归的理解. 所谓递归,可以这样理解,就是一个函数在自身的局部环境里通过自身函数名又调用, ...
- 递归神经网络(Recursive Neural Network, RNN)
信息往往还存在着诸如树结构.图结构等更复杂的结构.这就需要用到递归神经网络 (Recursive Neural Network, RNN),巧合的是递归神经网络的缩写和循环神经网络一样,也是RNN,递 ...
- javascript递归、循环、迭代、遍历和枚举概念
javascript递归.循环.迭代.遍历和枚举概念 〓递归(recursion)在数学与计算机科学中,是指在函数的定义中使用函数自身的方法.递归一词还较常用于描述以自相似方法重复事物的过程.例如,当 ...
- 【总结整理】javascript的函数调用时是否加括号
javascript的函数调用时是否加括号 if(event.preventDefault){ event.preventDefault(); if判断条件里面不要加括号,不加括号是应该以属性形式,i ...
- 【Python】利用递归函数调用方式,将所输入的字符串,以相反的顺序显示出来
源代码: """ 利用递归函数调用方式,将所输入的字符串,以相反的顺序显示出来 string_reverse_output():反向输出字符串的自定义函数 pending ...
- JavaScript 递归
递归是一种解决问题的方法,它解决问题的各个小部分,直到解决最初的大问题.通常涉及 函数调用自身. 能够像下面这样直接调用自身的方法或函数,是递归函数: var recursiveFunction = ...
- 提升JavaScript递归效率:Memoization技术详解[转载]
递归是拖慢脚本运行速度的大敌之一,太多的递归会让浏览器变得越来越慢直到死掉或者莫名其妙的突然自动退出.这里我们可以通过memoization技术来替代函数中太多的递归调用,提升JavaScript效率 ...
- javascript 递归之 快速排序
1. 快速排序思想 (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元素,都移到"基准"的左边:所有大 ...
- javascript 递归之阶乘
阶乘,即5! = 5*4*3*2*1, 先看传统的做法,利用while循环实现: function factorial(num){ var result = num; if(num<0){ re ...
随机推荐
- phpstorm调试环境XDebug搭建
1.安装XDebug,通过phpinfo()可以看到xdebug是否已安装!!!如果未安装,可以先看我的另一篇关于XDebug安装的文章:http://www.cnblogs.com/cxscode/ ...
- shell里面比较大小
#!/bin/bashif [ $1 -gt $2 ]then echo "$1>$2"else echo "$2>$1"fi# 数字判断一些命令 ...
- Selenium2+python自动化32- 测试报告的易读性【转载】
前言 前一篇已经介绍了报告的生成方法,本篇小编优化一下测试报告,使测试报告便于大多数阅读.虽然在我们在测试用例开发时为每个用例添加了注释,但测试报告一般是给非测试人员阅读的,如果能在报告中为每一个测试 ...
- 使用Pygame制作微信打飞机游戏PC版
前一阵子看了一篇文章:青少年如何使用Python开始游戏开发 .看完照葫芦画瓢写了一个,觉得挺好玩儿,相当于简单学了下Pygame库.这篇文章是个12岁小孩儿写的,国外小孩儿真心NB,想我12岁的时候 ...
- 在lua中正确使用uuid的方法:
-- 参考:http://ju.outofmemory.cn/entry/97724local function guid() local template ="xxxxxxx ...
- Ubuntu-16.04安装Xdebug-2.2.5及相关介绍
Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况.在日常开发中,我们会使用如 print_r() var_dump()等函数来进行调 ...
- linux--redis的安装和配置和开启多个端口
在workerman开发过程中需要安装redis来存储用户ip.端口等信息 首先UBUNTU中安装redis: apt-update //更新apt包源apt-get install redis-s ...
- 【互动问答分享】第10期决胜云计算大数据时代Spark亚太研究院公益大讲堂
“决胜云计算大数据时代” Spark亚太研究院100期公益大讲堂 [第10期互动问答分享] Q1:Spark on Yarn的运行方式是什么? Spark on Yarn的运行方式有两种:Client ...
- UVALive 3882.And Then There Was One-约瑟夫问题(递推)
And Then There Was One Time limit: 3.000 seconds Let’s play a stone removing game. Initially, n ston ...
- HDU 1285 确定比赛名次【字典序最小的拓扑排序 + 优先队列】
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...