函数柯里化???
这是什么???
干嘛用的???
解决什么问题???
我是谁???
我在那???
我要干什么???
我想大概只有面试的时候会用到吧!!!
毕竟,平时这么写程序的搬运工早已经被同胞干死了(你他特么是在秀技吗???)
废话不多说 ,先撸为敬
function carry(fn, args) {
// 当前传入函数的形参个数
var fnLength = fn.length
// 第一次args必定为空
var args = args || []
return function () {
// 获取最新的参数数组
let newArgs = [].slice.call(arguments).concat(args)
// let newArgs = Array.prototype.slice.call(arguments).concat(args)
// let newArgs = [...args, ...arguments]
// 以上三种方式都可以
// 如何目前已获取到的参数和形参参数相同说明是最后一次调用,需要返回结果
// 否则 ····
// 再次递归
if (newArgs.length >= fnLength) {
return fn.apply(this, newArgs)
} else {
return carry.call(this, fn, newArgs)
}
}
}
function add(a, b, c) {
return a + b + c
}
var muti = carry(add)
console.log(muti(1, 2, 3)) // 6
console.log(muti(1, 2)(3)) // 6
console.log(muti(1)(2, 3)) // 6
想明白了 就是这么简单 · · · · · ·
所以回过头来看它的作用· · · · · ·
然而并没有什么作用 · · · · ·