函数组合
函数组合
减少函数调用时函数的嵌套,可以让单个函数重复调用
// 函数组合的演示
function compose (f,g) {
return function (value) {
return f(g(value))
}
}
// 取数组中的最后一个元素
function reverse (array) {
return array.reverse()
}
function first (array) {
return array[0];
}
const last = compose(first, reverse)
console.log(last([0, 1, 2, 3]));