3 ms·
A bunch of these are cheating... just wrapping () in various ways or accessing call and apply in various ways. For general functions, I think the commenter who
by jmull 9y ago
A bunch of these are cheating... just wrapping () in various ways or accessing call and apply in various ways. For general functions, I think the commenter who says there are three ways to invoke a function is correct: (), call, apply.
There are additional ways specific functions are called -- I'm thinking of toString and valueOf.
- underwater 9y agoYeah, considering that `Function.prototype.apply === console.log.apply` there are a lot of dupes here. I would consider eval/new Function to be a legitimate fourth way though.
- jmull 9y agoYes, you have a good point about eval and anything else that boils down to invoking a string as a function.
- inimino 9y agoEval is another way to run arbitrary JS code. It's cheating here because nothing about the use of eval is specific to calling a function. According to that standard, opening an external interpreter would be yet another way. Agree with the above that there are really only three ways here.
- rimunroe 9y agoAs other people have pointed out in the gist, the syntax for tagged template literals gives an additional way to invoke a function foo`abc ${bar} ${baz}` This will call foo with the arguments [["abc ", " ", ""], bar, baz]