Skip to content Skip to sidebar Skip to footer

How To Implement Optional Parentheses During Function Call? (function Overloading)

My guess is that this is not possible, but I'd like f and f() to do the same thing. var f = function(str){ console.log(str||'foo'); }(); f; // wanted o

Solution 1:

No that is not possible. The parentheses are required to determine that the function should be called.

The value of the expression f() is the result of calling the function, while the value of f is the function itself (and f.tostring() if you display it).

Solution 2:

Not possible. The parentheses are how javascript knows that you want the method object to be executed.

Solution 3:

Remove the () from your def.

var f = function(str){ console.log(str||'foo'); };

Except your first case, all of them will work. You need to specify () to tell javascript to execute the function

Post a Comment for "How To Implement Optional Parentheses During Function Call? (function Overloading)"