当前位置:网站首页>手写柯里化 - toString 理解
手写柯里化 - toString 理解
2022-08-05 09:14:00 【InfoQ】
let arr = []
function addCurry() {
let arg = Array.prototype.slice.call(arguments); // 收集参数
arr = arr.concat(arg);
if (arg.length === 0) { // 如果参数为空,则判断递归结束
return arr.reduce((a,b)=>{return a+b}) // 求和
} else {
return addCurry;
}
}
addCurry(1)(2)(3)() // 6
Array.prototype.slice.call(arguments)arrreduce()addCurry(1)(2)(3)() // 6
addCurry(1)() // 7
function addCurry() {
let arr = [...arguments]
let fn = function () {
if(arguments.length === 0) {
return arr.reduce((a, b) => a + b)
} else {
arr.push(...arguments)
return fn
}
}
return fn
}
addCurry(1)(2)(3)
//或
addCurry(1)(2,3)
toStringlet fn = function(){}
fn.toString = () => 1
fn == 1 // true
function addCurry() {
let arr = [...arguments]
// 利用闭包的特性收集所有参数值
var fn = function() {
arr.push(...arguments);
return fn;
};
// 利用 toString 隐式转换
fn.toString = function () {
return arr.reduce(function (a, b) {
return a + b;
});
}
return fn;
}
addCurry(1)(2)(3) == 6 // true
边栏推荐
- sql server中 两表查询 平均数 分组
- Is there a problem with writing this?How to synchronize data in sql-client
- How to realize the short press and long press detection of the button?
- 无题十
- Luogu P1908: 逆序对 [树状数组]
- There is only one switch, how to realize the nqa of master-slave automatic switching
- (转)[Json]net.sf.json 和org.json 的差别及用法
- Luogu P4588: [TJOI2018]数学计算
- Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
- 15.1.1、md—md的基础语法,快速的写文本备忘录
猜你喜欢

mySQL数据库初始化失败,有谁可以指导一下吗

Example of Noise Calculation for Amplifier OPA855

express hot-reload

IT研发/开发流程规范效能的思考总结

DTcloud 装饰器

Why is pnpm hitting npm and yarn dimensionality reduction?

Thinking and summary of the efficiency of IT R&D/development process specification

并发之CAS

How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color

“充钱”也难治快手的“亏亏亏”?
随机推荐
How ali cloud storage database automatically to speed up the loading speed of www.cxsdkt.cn how to set up the case?
基因数据平台
十一道家常小菜详细攻略[图文并茂]
JS syntax usage
【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
营销建议 | 您有一份八月营销月历待查收! 建议收藏 !
leetcode 剑指 Offer 10- I. 斐波那契数列
全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
接口全周期的生产力利器Apifox
“充钱”也难治快手的“亏亏亏”?
openpyxl操作Excel文件
js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
PAT乙级-B1019 数字黑洞(20)
CCVR eases heterogeneous federated learning based on classifier calibration
无题十四
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
交换机端口的三种类型详解与hybrid端口实验
请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f
工程制图直线投影练习
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!