The same function summ :
summ(7)(3)(5) must equal 15
and
summ(7)(3)+5 must equal 15
and
summ(7)(3) must equal 10
How to make it possible?
You can use toString/valueOf method to treat result as a value.
function sum(a) {
chain.valueOf = function() {return a;}
return chain;
function chain(s) {
a += s;
return chain;
};
}
sum(7)(3)(5) == 15 // true
sum(7)(3) + 5 == 15 // true
+sum(7)(3)(5) // 15
valueOf makes more sense for non-string values, thoughvalueOf allow us to interpret this function as a value. +sum(7)(3) === 10, sum(7)(3) == 10. What more you need?+val is like 0+val just type conversion to numeric. sum(5) result has function type. It's why strict equality === fails. After type conversion with +sum(5) both side has number type and same value.
summshould return something that can optionally be treated as either a function or a number...?