I was told today that it's possible to invoke a function without parentheses. The only ways I could think of was using functions like apply or call.
f.apply(this);
f.call(this);
But these require parentheses on apply and call leaving us at square one. I also considered the idea of passing the function to some sort of event handler such as setTimeout:
setTimeout(f, 500);
But then the question becomes "how do you invoke setTimeout without parentheses?"
So what's the solution to this riddle? How can you invoke a function in Javascript without using parentheses?
7 Answers
There are several different ways to call a function without parentheses.
Let's assume you have this function defined:
function greet() {
console.log('hello');
}
Then here follow some ways to call greet without parentheses:
1. As Constructor
With new you can invoke a function without parentheses:
new greet; // parentheses are optional in this construct.
From MDN on the new oprator:
Syntax
new constructor[([arguments])]