Invoking a function without parentheses
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])]
2. As toString
or valueOf
Implementation
toString
and valueOf
are special methods: they get called implicitly when a conversion is necessary:
var obj = {
toString: function() {
return 'hello';
}
}
'' + obj; // concatenation forces cast to string and call to toString.
You could (ab)use this pattern to call greet
without parentheses:
'' + { toString: greet };
Or with valueOf
:
+{ valueOf: greet };
valueOf
and toString
are in fact called from the @@toPrimitive method (since ES6), and so you can also implement that method:
+{ [Symbol.toPrimitive]: greet }
"" + { [Symbol.toPrimitive]: greet }
2.b Overriding valueOf
in Function Prototype
You could take the previous idea to override the valueOf
method on the Function
prototype:
Function.prototype.valueOf = function() {
this.call(this);
// Optional improvement: avoid `NaN` issues when used in expressions.
return 0;
};
Once you have done that, you can write:
+greet;
And although there are parentheses involved down the line, the actual triggering invocation has no parentheses. See more about this in the blog "Calling methods in JavaScript, without really calling them"
3. As Generator
You could define a generator function (with *
), which returns an iterator. You can call it using the spread syntax or with the for...of
syntax.
First we need a generator variant of the original greet
function:
function* greet_gen() {
console.log('hello');
}
And then we call it without parentheses by defining the @@iterator method:
[...{ [Symbol.iterator]: greet_gen }];
Normally generators would have a yield
keyword somewhere, but it is not needed for the function to get called.
The last statement invokes the function, but that could also be done with destructuring:
[,] = { [Symbol.iterator]: greet_gen };
or a for ... of
construct, but it has parentheses of its own:
for ({} of { [Symbol.iterator]: greet_gen });
Note that you can do the above with the original greet
function as well, but it will trigger an exception in the process, after greet
has been executed (tested on FF and Chrome). You could manage the exception with a try...catch
block.
4. As Getter
@jehna1 has a full answer on this, so give him credit. Here is a way to call a function parentheses-less on the global scope, avoiding the deprecated __defineGetter__
method. It uses Object.defineProperty
instead.
We need to create a variant of the original greet
function for this:
Object.defineProperty(window, 'greet_get', { get: greet });
And then:
greet_get;
Replace window
with whatever your global object is.
You could call the original greet
function without leaving a trace on the global object like this:
Object.defineProperty({}, 'greet', { get: greet }).greet;
But one could argue we do have parentheses here (although they are not involved in the actual invocation).
5. As Tag Function
Since ES6 you can call a function passing it a template literal with this syntax:
greet``;
See "Tagged Template Literals".
6. As Proxy Handler
Since ES6, you can define a proxy:
var proxy = new Proxy({}, { get: greet } );
And then reading any property value will invoke greet
:
proxy._; // even if property not defined, it still triggers greet
There are many variations of this. One more example:
var proxy = new Proxy({}, { has: greet } );
1 in proxy; // triggers greet
7. As instance checker
The instanceof
operator executes the @@hasInstance
method on the second operand, when defined:
1 instanceof { [Symbol.hasInstance]: greet } // triggers greet
The easiest way to do that is with the new
operator:
function f() {
alert('hello');
}
new f;
While that's unorthodox and unnatural, it works and is perfectly legal.
The new
operator doesn't require parentheses if no parameters are used.
You can use getters and setters.
var h = {
get ello () {
alert("World");
}
}
Run this script just with:
h.ello // Fires up alert "world"
Edit:
We can even do arguments!
var h = {
set ello (what) {
alert("Hello " + what);
}
}
h.ello = "world" // Fires up alert "Hello world"
Edit 2:
You can also define global functions that can be run without parenthesis:
window.__defineGetter__("hello", function() { alert("world"); });
hello; // Fires up alert "world"
And with arguments:
window.__defineSetter__("hello", function(what) { alert("Hello " + what); });
hello = "world"; // Fires up alert "Hello world"
Disclaimer:
As @MonkeyZeus stated: Never ever shall you use this piece of code in production, no matter how good your intentions.