柯里化通用函数
function curring(fn,args=[]){
let l = fn.length
return function(...arg){
let newArgs = [...arg,...args]
if(newArgs.length == l){
return fn(...newArgs)
}else{
return curring(fn,newArgs)
}
}
}
function sum(a,b,c,d){
return a+b+c+d
}
let newFn = curring(sum)
console.log(newFn(1)(2)(3)(4))
再介绍下切片函数(因为和朋友讨论这个)
let fn2 = function (){
return 456
}
let fn1 = function(...arg){
return arg
}
fn1.__proto__.before = function (fn) {
let that = this;
return function () {
let a = fn2()
return fn.call(that,...arguments,a);
}
}
let newFn = fn1.before(fn1);
console.log(newFn('123'))