``````add(2)(3)(4) //输出9
``````

``````function add(x) {
var sum = x;
var tmp = function (y) {
sum = sum + y;
return tmp;
};
tmp.toString = function () {
return sum;
};
return tmp;
}
``````

``````    var tmp = function (y) {
sum = sum + y;
return tmp;
};
tmp.toString = function () {
return sum;
};
return tmp;
``````

## 回答

``add(2)(3)(4) //输出9``

A(3)返回函数B
B(4)返回函数C

``````function add(num){
var sum=0;
sum= sum+num;
return function(numB){
sum= sum+ numB;
return function(numC){
sum= sum+ numC;
return sum;
}
}
}``````

``var result=add(2)(3)(4);``

`然后再考虑他的拓展性 `

``var result=add(2)(3)(4)(5);``

``````function add(num){
var sum=0;
sum= sum+num;
return function(numB){
sum= sum+ numB;
return function(numC){
sum= sum+ numC;
return function(numD){
sum= sum+ numD;
return function....
};
}
}
}``````

``````function add(num){
var sum=0;
sum= sum+num;
return function tempFun(numB){
sum= sum+ numB;
return tempFun;
}
}``````

``````var result=add(2)(3)(4)(5);
console.log(result);``````

2种方法

``````function add(num){
var sum=0;
sum= sum+num;
return function tempFun(numB){
if(arguments.length===0){
return sum;
}else{
sum= sum+ numB;
return tempFun;
}

}
}``````

``````var result=add(2)(3)(4)(5)();
console.log(result);//输出14``````

``````function add(num){
var sum=0;
sum= sum+num;
var tempFun=function(numB){
if(arguments.length===0){
return sum;
}else{
sum= sum+ numB;
return tempFun;
}

}

tempFun.valueOf=function(){
return sum;
}
tempFun.toString=function(){
return sum+'';
}

return tempFun;
}``````
``````var result=add(2)(3)(4)(5);
console.log(+result);//输出14 valueOf
console.log(result);//输出14 toString``````

PS:还可以这样实现

``````var add=(function(){
var args=[];
if(arguments.length===0){
return calResult;
}else{
Array.prototype.push.apply(args,Array.prototype.splice.call(arguments,0));
}

}
function calResult(){
var result=args.reduce(function(previousValue, currentValue){
return previousValue+currentValue;
},0);
args=[];
return result;
}
return calResult();
};

return calResult()+'';
};

}());

``````function curry(fn) {
var slice = [].slice;
var len = fn.length;

return function curried() {
var args = slice.call(arguments);
if (args.length >= len) {
return fn.apply(null, args);
}

return function () {
return curried.apply(null, args.concat(slice.call(arguments)));
};
};
}

var add = curry(function (a, b, c, d) {
return a + b + c + d;
});

``````var curry = function(final, arity) {
var curried = function() {
// this是每次的参数列表
// 每次slice()保证curry后的函数仍然是无状态的
var new_args = this.slice();
for(arg_key in arguments) {
new_args.push(arguments[arg_key]);
}

if (new_args.length >= arity) {
return final.apply(null, new_args);
} else {
return curried.bind(new_args);
}
};

return curried.bind([]);
};

var sum4 = function(a,b,c,d) { return a+b+c+d; };

var curried = curry(sum4, sum4.length);

console.log(curried(1,2,3)(4));
// -> 10
console.log(curried(1)(2,3)(4));
// -> 10``````

``````function add(x) {
var sum = x;
var tmp = function (y) {
sum = sum + y;
return tmp;
};
tmp.toString = function () {
return sum;
};
return tmp;
}

``````var a = add(1)(2)(3);
console.log( typeof a ); // function``````

``````function add(n) {
var fn = function(m) {
};

fn.valueOf = function() {
return n;
};

fn.toString = function() {
return '' + n;
};

return fn;
}``````

``````var tmp = function (y) {
sum = sum + y;
return tmp;
};``````

``````    tmp.toString = function () {
return sum;
};``````

``````    if(arguments.length == 1){
return function(y) { return x + y; };
}else{
return x+r;
}
};
``````

``````var currying = function (fn) {
let args = [];
return function() {
[].push.apply(args, arguments);
return fn.apply(null, args);
}
}
let num = Array.from(arguments)
return num.reduce((x, y) => {
return x + y
})
})
var ride = currying(function() {
let num = Array.from(arguments)
return num.reduce((x, y) => {
console.log('ride', num)
return x * y
})
})
console.log(ride(1, 2, 5))
console.log(ride(10))``````

``````function add(p) {
var result = p;

return add = function (p1) {

if (p1) {
result += p1;
} else {
return result;
}
}
}``````

``````function sum(num){
let sumNum = 0;
let args = [].slice.call(arguments);
sumNum += args.reduce(function(a,b){
return a+b;
})
let curryFun = function(numB){
let argsF = [].slice.call(arguments);
if(arguments.length === 0){
return sumNum;
}else {
sumNum += argsF.reduce(function(a,b){
return a+b;
})
return curryFun;
}
};

curryFun.valueOf = function(){
return sumNum;
}
curryFun.toString = function(){
return sumNum;
}
return curryFun;
}

let x = 2;
let y = 5;
console.log(sum(1,3));
console.log(sum(1)(3));
console.log(sum(1)(3,4)(5));``````