I don’t understand how f.toString (without calling it) evaluate to returned result of that same function ?
function f(a){
console.log(a + a);
f.toString = function(){return a};// this part is
//confusing.
return f;
};
f(10) // 20
f(10)(20)// 20 40
In fact
f.toStringis not even called in your scenario. If you will remove this line – result will be the same.f.toString()will be called if you try to executeconsole.log(f);Your results explained:
function
fis called with argument10, and it prints to consoleconsole.log(10 + 10);first part as above, but
ffunction also returns itselfreturn f;, it means that second part(20)will be treated as call to the same function one more time and it printsconsole.log(20 + 20);