手写组合函数
作者:互联网
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));
标签:function,index,fns,函数,组合,length,let,result,手写 来源: https://www.cnblogs.com/wayhome123/p/15575513.html