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 ...
随机推荐
- GitLab版本管理【转】
转自:http://www.cnblogs.com/wintersun/p/3930900.html GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的Git项目 ...
- 带你入门代理模式/SpringAop的运行机制
SpringAop 是spring框架中最重要的一项功能之一,同时也是企业级开发记录事物日志等不可或缺的一部分,如果说你的系统需要记录用户访问接口的操作,那SpringAop是很完美的了,当然,拦截器 ...
- springmvc formatter
以下,来自于Springmvc指南第二版,第93页. Spring的Formatter是可以将一种类型转为另一种类型. 例如用户输入的date类型可能有多种格式. 下面是才用 registrar方式注 ...
- [ Openstack ] Openstack-Mitaka 高可用之 Dashboard
目录 Openstack-Mitaka 高可用之 概述 Openstack-Mitaka 高可用之 环境初始化 Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...
- 《Java编程思想》笔记 第十七章 容器深入研究
1 容器分类 容器分为Collection集合类,和Map键值对类2种 使用最多的就是第三层的容器类,其实在第三层之上还有一层Abstract 抽象类,如果要实现自己的集合类,可以继承Abstract ...
- rest_framework 访问频率(节流)流程
访问频率流程 访问频率流程与认证流程非常相似,只是后续操作稍有不同 当用发出请求时 首先执行dispatch函数,当执行当第二部时: #2.处理版本信息 处理认证信息 处理权限信息 对用户的访问频率进 ...
- python 多进程并发与多线程并发
本文对python支持的几种并发方式进行简单的总结. Python支持的并发分为多线程并发与多进程并发(异步IO本文不涉及).概念上来说,多进程并发即运行多个独立的程序,优势在于并发处理的任务都由操作 ...
- 【转】如何只用CSS做到完全居中
英文原版链接:http://codepen.io/shshaw/full/gEiDt 我们都知道 margin:0 auto; 的样式能让元素水平居中,而 margin: auto; 却不能做到垂直居 ...
- <松本行弘的程序世界> 读书笔记
第一章 编程语言不是从安全性的角度考虑以减少程序员犯错误,而是在程序员自己负责的前提下为他提供最大限度发挥能力的灵活性. 第二章 根据对象的不同类型而进行适当的处理,就是多态性的基本内容.根据数据类型 ...
- codeforces Round 442 B Nikita and string【前缀和+暴力枚举分界点/线性DP】
B. Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes input standar ...