定义颗粒度函数
2020-05-26 本文已影响0人
踏雪_739a
// 定义颗粒度函数 当颗粒度参数总个数小于调用函数的参数个数就 生成函数 直到总参数个数相等调用原函数
function currying(callback, arr = []){
let argsNumber = callback.length
return (...args)=>{
let newArr = [...arr, ...args]
if(newArr.length < argsNumber){
return currying(callback, newArr)
}else{
return callback(...newArr)
}
}
}
function sum(a,b,c,d,e){
return a+b+c+d+e
}
let res = currying(sum)(1,2)(3,4)(5)
console.log(res)
function isTpye(type,value){
return Object.prototype.toString.call(value) === `[object ${type}]`
}
let isArr = currying(isTpye)("Array")
let isArr0 = isArr([1,2])
let isArr1 = isArr('sss')
console.log(isArr0)
console.log(isArr1)