我
实现一个累加函数的功能比如 sum(1,2,3)(2).valueOf()
难度:
js
function sum(...args) {
let result = 0;
result = args.reduce(function (pre, item) {
return pre + item;
}, 0);
let add = function (...args) {
result = args.reduce(function (pre, item) {
return pre + item;
}, result);
return add;
};
add.valueOf = function () {
console.log(result);
};
return add;
}