Last active
August 19, 2024 12:23
-
-
Save myshov/05800f083a0afce56e0f782314a103eb to your computer and use it in GitHub Desktop.
11 Ways to Invoke a Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
do not forget constructor without parenthesis so mainly 5 way.
function A () {};
new A;
A``;
A();
A.call();
A.apply();
with double colon bind operator:
console::(console.log)(0)
Promise.resolve(19).then(console.log)
console["log"](9)
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';
This thread has some interesting examples: https://stackoverflow.com/questions/35949554/invoking-a-function-without-parentheses
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
Does this count?