前言
有這樣的一個(gè)需求:輸入test(fun1, fun2, fun3...)隧土,要求按照f(shuō)un1(fun2(fun3(...)))的順序執(zhí)行函數(shù)。
正文
其實(shí)這就是一個(gè)函數(shù)組合的問(wèn)題
//函數(shù)組合
//需求:輸入test(a,b,c,d,e,f....)轻局,執(zhí)行a(b(c(d(e(f)))))
function compose () {
let argus = arguments
let start = arguments.length - 1
return function () {
let i = start
let result = argus[i].apply(null, arguments)
while (i--) {
result = argus[i].call(null, result)
}
return result
}
}
function a (x) {
console.log(x+1)
}
function b (x) {
return x * 2
}
let composefun = compose(a, b)
composefun(1)