functioncompose(){
var args = arguments;
var first = arguments.length-1;
returnfunction(){
var result = args[first].apply(null, arguments);
var i = first - 1;
while(i>=0){
result = args[i].call(null, result);
i--;
}
return result;
}
}
const add = (x, y) => x + y + 4;
const mulatity = x => x * 4;
const toString = x => String(x);
const mySplit = arr => arr.split(",");
const myConsole = x => console.log(x);
const test = compose(myConsole, toString, mulatity, add);
test(1,3);
var test = function(){
var result = args[first].apply(null, arguments);
var i = first - 1;
while(i>=0){
result = args[i].call(null, result);
i--;
}
return result;
}
然后我们传入(1,3);让我们看看 result 返回的是什么?
args[first].apply(null,arguments)=>等于调用 result = add(1,3)=>1+3+4;
result = 8;