function sum(a) {
var currentSum = a;
function f(b) {
currentSum += b;
return f;
}
f.toString = function() {
return currentSum;
};
return f;
}
var sum = (function() {
var currentSum;
return function(a) {
if(a === undefined) {
return currentSum
}
currentSum = a;
function f(b) {
currentSum += b;
return f;
}
f.toString = function() {
return currentSum;
};
return f;
}
}())
alert( sum(1)(2) ); // 3
alert( sum(5)(-1)(2) ); // 6
alert(sum()); // 6