Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
11 Ways to Invoke a Function
(_ => console.log(2))();
eval('console.log(3);');, 4);
console.log.apply(null, [5]);
new Function('console.log(6)')();
Reflect.apply(console.log, null, [7])
Reflect.construct(function(){console.log(8)}, []);, null, [9]);, null, 10);
new (require('vm').Script)('console.log(11)').runInThisContext();
Copy link

SpencerJobe commented Apr 9, 2017

Does this count?

var i = document.createElement("img");
i.onerror = function() { console.log(1); };
i.src = "nope";

Copy link

garaboncias commented Apr 9, 2017

do not forget constructor without parenthesis so mainly 5 way.
function A () {};
new A;

Copy link

gonzaloruizdevilla commented Apr 11, 2017

with double colon bind operator:

Copy link

jorrit commented Apr 11, 2017


Copy link

guillermo commented Jun 18, 2017


Copy link

franciscop commented Jun 18, 2017

Using Proxy (not yet mentioned) you can make accessing, setting, etc a property to be an actual function call:

const log = new Proxy(console.log, {
  get: (orig, key) => orig(key)


Based on this awesome/awful feature I created a little monster:

const buttons = dom.button.html;
dom.a.html = 'Hello world';

Copy link

jonbri commented Jun 19, 2017

Copy link

crisdosyago commented Jun 19, 2017

I'm a fan of this one:

(class { [console.log(1)](){} })
(function z( a = console.log(1) ){})()
[...{[Symbol.iterator](){return {next(){ return {done:console.log(1)}}}}}]; // hehe
try{[1,2,3][~~(Math.random()*4)].toString()}catch(e){console.log(12)}; // nondeterministically run console.log 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment