I am learning typescript in typescript exercise
in this subject solution,there is a function:
function toFunctional<T extends Function>(func: T): Function {
const fullArgCount = func.length;
function createSubFunction(curriedArgs: unknown[]) {
return function(this: unknown) {
const newCurriedArguments = curriedArgs.concat(Array.from(arguments));
if (newCurriedArguments.length > fullArgCount) {
throw new Error('Too many arguments');
}
if (newCurriedArguments.length === fullArgCount) {
return func.apply(this, newCurriedArguments);
}
return createSubFunction(newCurriedArguments);
};
}
return createSubFunction([]);
}
I dont understand this line:
const fullArgCount = func.length;
cause in that after,parameter func is a function,but whats the value of func.length ?
and whats newCurriedArguments actually mean ?
and whats newCurriedArguments actually mean ?
const newCurriedArguments = curriedArgs.concat(Array.from(arguments));
newCurriedArguments is the result of concatenating two arrays:
curriedArgs from createSubFunction() function, as the inner function where newCurriedArguments is declared has access to its parent function variables, and curriedArgs is one of them.arguments which is the arguments of the function itself.So you could say newCurriedArguments is the arguments of both the function and its parent.