Skip to content

Instantly share code, notes, and snippets.

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

davidmarkclements commented Apr 8, 2017

@pwang2 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals

another form of IIFE (makes function into expression) but I think it's semantically nicer:

void function () { console.log('19') }()

Also in terms of "hidden" or unexpected execution:

var o = {toString: () => console.log('20')}
var n= +o //20
~o //20
o + ''  //20
var s = `${o}` //20
o[o] //20
parseFloat(o, 10) //20
o++ //20
var o = {valueOf: () => console.log('21')}
var n= +o //21
~o //21
o + ''  //21
var s = `${o}` //21
o[o] //21
parseFloat(o, 10) //21
o++ //21
var o = {[Symbol.toPrimitive]: () => console.log('22')}
var n= +o // 22
~o //22
o + '' //22
var s = `${o}` //22
o[o] //22
parseFloat(o, 10) //22
o++ //22

node only:

var o = {inspect: () => console.log('23')}
console.log(o) // 23

You can also make functions calls for any object (that isn't pure - e.g. inheriting from null)

Object.prototype.toString = () => { console.log('24') }
Object.prototype.valueOf = () => { console.log('25') }
parseFloat({}, 10) //24
''[{}] // 24
1 + {} // 25
1 + function () {} // 25
Object.prototype[Symbol.toPrimitive] = () => { console.log('26') }
parseFloat({}, 10) //26
''[{}] // 26
1 + {} // 26
1 + function () {} // 26

And then there's meta programming for side effect calls:

var p = new Proxy({}, {
  has: (t, k) => {
    console.log('27')
    return true
  }
})


if ('foo' in p) {}  // 27
with (p) {
  moo + shoe //27 27
}

Object.prototype.hasOwnProperty = new Proxy(Object.prototype.hasOwnProperty, {
  apply: (t, self, args) => {
    console.log('28')
    return t.apply(self, args)
  }
})

var o = {}

if (o.hasOwnProperty('a')) {} // 28

@davidmarkclements
Copy link

davidmarkclements commented Apr 8, 2017

As one liner "function calls":

+{[Symbol.toPrimitive]: () => console.log('x')}
+{toString: console.log.bind(console, 'x')}
+{valueOf: console.log.bind(console, 'x')}
'x' in (new Proxy({}, {has: () => console.log('x')}))
with (new Proxy({}, {has: () => (console.log('x'),1)})) callThatThing

More:

({get x() { console.log('x') }}).x
Object.create(null, {x: {get: () => console.log('x')}}).x

v8 only (node, chrome):

Error.prepareStackTrace = () => console.log('x'); Error()
Error.prepareStackTrace = () => console.log('x'); try { u } catch(e) { e }

@SpencerJobe
Copy link

SpencerJobe commented Apr 9, 2017

Does this count?

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

@garaboncias
Copy link

garaboncias commented Apr 9, 2017

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

@gonzaloruizdevilla
Copy link

gonzaloruizdevilla commented Apr 11, 2017

with double colon bind operator:
console::(console.log)(0)

@jorrit
Copy link

jorrit commented Apr 11, 2017

Promise.resolve(19).then(console.log)

@guillermo
Copy link

guillermo commented Jun 18, 2017

console["log"](9)

@franciscop
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)
});

log[11];

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

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

@jonbri
Copy link

jonbri commented Jun 19, 2017

@crisdosyago
Copy link

crisdosyago commented Jun 19, 2017

I'm a fan of this one:

(class { [console.log(1)](){} })
({[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