implementation of function Corialization seen on the Internet js Advanced tutorial
about implementing an add method, the calculation results can satisfy the following expectations:: add (1) (2) (3) = 6 add (1, 2, 3) (4) = 10 add (1) (2) (3) (4) (5) = 15
function add() {
//
var _args = [].slice.call(arguments);
// _args
var adder = function () {
var _adder = function() {
[].push.apply(_args, [].slice.call(arguments));
return _adder;
};
//
_adder.toString = function () {
return _args.reduce(function (a, b) {
return a + b;
});
}
return _adder;
}
return adder.apply(null, [].slice.call(arguments));
}
//
console.log(add(1, 2, 3, 4, 5)); // 15
console.log(add(1, 2, 3, 4)(5)); // 15
console.log(add(1)(2)(3)(4)(5)); // 15
I don"t understand the function _ adder that collects parameters, so why does it return itself, and it"s not a recursive call? After I remove this sentence, I will report an error
add (.) (.). Is not a function