当前位置:网站首页>es6递归函数
es6递归函数
2022-08-09 11:09:00 【一头小驴】
首先看代码:
function tco(f) {
var value;
var active = false;
var accumulated = [];
return function accumulator() {
accumulated.push(arguments);
if (active ==false) {
active = true;
while (accumulated.length>0) {
console.log(accumulated);
console.log(accumulated.length);
value = f.apply(this, accumulated.shift());
}
active = false;
return value;
}
};
}
var ff = function(x, y) {
if (y > 0) {
return sum(x + 1, y - 1)
}
else {
return x
}
}
var sum = tco(ff);
console.log(sum);
var tt = sum(1, 5);
console.log(tt);
如上,是一个化递归为 while(true)循环的例子,然后 就是 apply方法 在传递参数的时候,会给上层 函数体的参数arguments赋值 从而 维持了while(true),可以验证。
(google f12开发者工具 source中调试js代码,断点,逐步,参数变量显示等等,还有控制台写代码的智能提示)
边栏推荐
- 【Subpixel Dense Refinement Network for Skeletonization】CVPR2020论文解读
- 排序--快排(图解)
- How tall is the B+ tree of the MySQL index?
- Use gdb to debug multi-process programs, debug parent and child processes at the same time
- 富媒体在客服IM消息通信中的秒发实践
- UNIX哲学
- STM32启动方式及BootLoader
- Invisible OOM in kubernetes
- matlab fcnchk 函数用法
- faster-rcnn learn
猜你喜欢
随机推荐
MySQL查询性能优化七种武器之索引潜水
Beauty Values
Getting Started with MNIST Machine Learning
Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
margin出bug---margin失效
数论知识点
x86异常处理与中断机制(2)中断向量表
性能测试(04)-表达式和业务关联-JDBC关联
x86异常处理与中断机制(1)概述中断的来源和处理方式
爱可可AI前沿推介(8.9)
二进制加法
PTA 计算天数
信号量SIGCHLD的使用,如何让父进程得知子进程执行结束,如何让父进程区分多个子进程的结束
Looper 原理浅析
MDK添加注释模板
二叉树 前序是根在前(根左右)中序(左根右)
threejs+shader 曲线点运动,飞线运动
Julia常见符号意思
vite的原理,手写vite
enum in c language