flow接收多个函数作为参数,将第N个函数的返回值作为参数传给第N+1个函数。
function flow(funcs) {
const length = funcs ? funcs.length : 0
let index = length
while (index--) {
if (typeof funcs[index] != 'function') {
throw new TypeError('Expected a function')
}
}
return function(...args) {
let index = 0
let result = length ? funcs[index].apply(this, args) : args[0]
while (++index < length) {
result = funcs[index].call(this, result)
}
return result
}
}
export default flow
前8行判断是否为函数。
11行三目运算判断参数列表是否为0
apply()
方法调用一个函数, 其具有一个指定的this
值,以及作为一个数组提供的参数。
循环判断----倚天屠龙
call() 方法调用一个函数, 其具有一个指定的this值和分别地提供的参数(参数的列表)。