compose,currying
compose函数合成 f(g(h(x))) = ff(x) currying函数柯里化 将函数多个入参,改成一个入参
// 递归 单参
function compose1(fns) {
function getVal(fns, start, val) {
if (!fns) {return}
let first = fns[start]
let second = fns && fns[start + 1]
if (first && !second) {
return first(val)
}
if (first && second && start + 1 < fns.length) {
return first(second(val))
} else {
return first(getVal(fns, start + 1, val))
}
}
return function(val) {
return getVal(fns, 0, val)
}
}
// reduce 单参
function compose2(fns) {
return function(val) {
return fns.reduce((lastVal, curFn) => {return curFn(lastVal)})
}
}
// reduce 多参
function compose3(fns) {
return fns.reduce((a, b) => {return (...args) => {a(b(...args))}})
}
function currying() {
}
call,apply,bind
bind返回new function,闭包 call,apply直接调用,2者传参方式有区别