``````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;
``````

13 个回答

``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;
}