手写组合函数
function whCompose(...fns) {
let length = fns.length;
for (let i = 0; i < length; i++) {
if (typeof fns[i] !== 'function') {
throw new TypeError('Expected params are functions');
}
}
return function com(...params) {
let index = 0;
let result = length ? fns[index].apply(this, params) : params;
while (++index < length) {
result = fns[index].apply(this, [result]);
}
return result;
};
}
function double(n) {
return n * 2;
}
function square(m) {
return m ** 2;
}
let fn = whCompose(double, square);
console.log(fn(1));
注:以上内容仅用于日常学习