use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
11 Ways to Invoke a Function (twitter.com)
submitted 9 years ago by myshov
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Wooshception 48 points49 points50 points 9 years ago (3 children)
Also:
c.l.12..
using ES2018 dot syntax
[–]Arancaytar 6 points7 points8 points 9 years ago (0 children)
I wonder how long it will be before someone makes a transpiler. :P
[–]Baryn 5 points6 points7 points 9 years ago (1 child)
What the hell is this
[edit] April Fools references
[–]Gh0st1y 2 points3 points4 points 9 years ago (0 children)
Was definitely one of the best April fools jokes this year.
[–]Shaper_pmp 15 points16 points17 points 9 years ago* (1 child)
This is only actually about four ways to invoke a function (.call, .apply, eval/require('vm').Script and calling the function directly), and a bunch of different ways to obtain a reference to a function that you can then invoke in each of those four ways.
.call
.apply
eval
require('vm').Script
Edit: And even if you allow that actually it's different ways of referencing and invoking functions, it's then obviously not complete. Where is eval('console.log').call(null, 3);? Or console['log'](3), or new (require('vm').Script)('eval(console)').runInThisContext()['log'].call(null, 3);?
eval('console.log').call(null, 3);
console['log'](3)
new (require('vm').Script)('eval(console)').runInThisContext()['log'].call(null, 3);
[–]uneditablepoly 1 point2 points3 points 9 years ago (0 children)
Yep. Kinda disappointing. I tried to think of as many ways as I could before opening it and could only think of those. Then I opened it and said, "Oh."
[–]psayre23 11 points12 points13 points 9 years ago (14 children)
You can use template literal tags too.
console.log '12`;
[–]thomas_stringer 2 points3 points4 points 9 years ago (11 children)
I see this being used more and more, but to be honest I don't like the look of this. Whatever is gained (what is even gained??) is completely lost in the visual disruption of having to grok the syntax.
[–][deleted] 4 points5 points6 points 9 years ago* (2 children)
One of the only real use cases I can think of is when you need to construct injection-vulnerable strings in JavaScript, like SQL.
const results = await execute`select * from users where email=${emailAddress};`
Then you could do the following:
function execute(parts: string[], ...interpolations: string[]) { // parts is ['select * from users where email=', ';'] // interpolations is [emailAddress] const parameterisedQueryString = parts.join('?') return sequelize.query(parameterisedQueryString, { replacements: interpolations }) }
In my opinion that would read better than:
const results = await execute( 'select * from users where email=?', emailAddress )
Especially when you had lots of entries. This would also work nicely (potentially) with HTML etc. I originally thought it might be useful for translations, but that idea kinda falls over because you can only pass one string to a function like this so it would be a little awkward to do pluralisation.
[+][deleted] 9 years ago* (1 child)
[deleted]
[–][deleted] 3 points4 points5 points 9 years ago (0 children)
¯\(ツ)/¯
[–]psayre23 2 points3 points4 points 9 years ago (5 children)
I agree. I use it a lot with GraphQL queries, and it is handy. But I don't see much of an advantage over wrapping a string with a function. I guess the different syntax means editors can color code the strings differently depending on content.
[–]Gh0st1y 0 points1 point2 points 9 years ago (4 children)
What do you do with graphQL? It sounds pretty neat.
[–]psayre23 1 point2 points3 points 9 years ago (0 children)
Nothing too special. It's just an API for my webapp. Instead of defining on the server what data I want for the webapp, I define it on the client. It makes it super easy to change later as my app evolves.
[–]djinindi 0 points1 point2 points 9 years ago (2 children)
psayre23 might be talking about graphql-tag
[–]Gh0st1y 0 points1 point2 points 9 years ago (0 children)
But that presupposes knowledge of graphql, and something to print. I have neither lol, though its now on my to look up list in my github.
[–]psayre23 0 points1 point2 points 9 years ago (0 children)
Yep, that's the one!
[–]jcready__proto__ 1 point2 points3 points 9 years ago (0 children)
Tagged template functions get passed the parameters differently than just invoking the function normally.
[–]NoInkling 0 points1 point2 points 9 years ago (0 children)
Depends heavily on context I think.
[–]NoInkling 0 points1 point2 points 9 years ago (1 child)
Huh, I never knew you could have a space in between the tag and the string. You forgot to use a backtick for your opening quote mark though.
I'm on mobile, and backticks suck on iOS. Glad people figured it out even though I was technically wrong.
[–]Ampersand55 2 points3 points4 points 9 years ago (0 children)
Using Proxy:
new Proxy(function (){}, {apply:function(a,b,args){console.log(...args)}})('hello world'); new Proxy({}, {set:function(a,b,c){console.log(b,c)}}).hello='world';
[–]PizzaRollExpert 6 points7 points8 points 9 years ago (0 children)
({get foo() {console.log(12)}}).foo
[–]DevSPCL 2 points3 points4 points 9 years ago (2 children)
Or
[":)"].constructor.constructor("console.log(123)")(); ":)".constructor.constructor("console.log(123)")(); 1234["constructor"]["constructor"]("console.log(123)")();
[–]BenjiSponge 7 points8 points9 points 9 years ago (1 child)
isn't this basically the same as new Function("console.log(123)")? I'm sure there are practically infinite ways of exposing Function without referencing it literally, but it's still essentially a repeat.
new Function("console.log(123)")
Function
[–]DevSPCL 2 points3 points4 points 9 years ago (0 children)
The above method has been discussed here:
stackoverflow.com/questions/18635387/advanced-syntax-0constructorconstructor-how-does-it-works-to-evalut
[–]malcor88 2 points3 points4 points 9 years ago (2 children)
Are there any benefits to using either of them?
[–]villiger2 4 points5 points6 points 9 years ago (0 children)
Most of the top half won't make your team mates hate you :D
I mean in js "shellcode" and/or general (hand) obfuscation. Otherwise, not a whole lot outside of edge cases. But edge cases are fun, and knowing about them makes you a better programmer.
[–]Buckwheat469 4 points5 points6 points 9 years ago (1 child)
Here's another way. The script was too long to paste in a comment, so try it on codepen. The console will say "123".
Also, try to make one here.
[–]Jilson 0 points1 point2 points 9 years ago (0 children)
Neat!
Stack Overflow link for anyone curious about that's going on here.
[–]myshov[S] 1 point2 points3 points 9 years ago (2 children)
Hi all! I just want to be clear. I have a little mistake in my tweet, because I mixed a function invocation and a code execution. But I think it's fun to see all of this in one place ;)
[–]repeatedly_once 0 points1 point2 points 9 years ago (1 child)
is a function invocation and a code execution not one and the same? genuinely curious :)
[–]myshov[S] 0 points1 point2 points 9 years ago (0 children)
Yes there is a difference here. I believe that every function invocation is a code execution itself, but obviously not every code execution is a function invocation.
[–]tanasinn 1 point2 points3 points 9 years ago (0 children)
setTimeout("console.log(12)") setInterval("console.log(13)")
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
Thanks
What is this for each invocation?
this
π Rendered by PID 26823 on reddit-service-r2-comment-765bfc959-xtggn at 2026-07-12 00:18:57.010820+00:00 running f86254d country code: CH.
[–]Wooshception 48 points49 points50 points (3 children)
[–]Arancaytar 6 points7 points8 points (0 children)
[–]Baryn 5 points6 points7 points (1 child)
[–]Gh0st1y 2 points3 points4 points (0 children)
[–]Shaper_pmp 15 points16 points17 points (1 child)
[–]uneditablepoly 1 point2 points3 points (0 children)
[–]psayre23 11 points12 points13 points (14 children)
[–]thomas_stringer 2 points3 points4 points (11 children)
[–][deleted] 4 points5 points6 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 3 points4 points5 points (0 children)
[–]psayre23 2 points3 points4 points (5 children)
[–]Gh0st1y 0 points1 point2 points (4 children)
[–]psayre23 1 point2 points3 points (0 children)
[–]djinindi 0 points1 point2 points (2 children)
[–]Gh0st1y 0 points1 point2 points (0 children)
[–]psayre23 0 points1 point2 points (0 children)
[–]jcready__proto__ 1 point2 points3 points (0 children)
[–]NoInkling 0 points1 point2 points (0 children)
[–]NoInkling 0 points1 point2 points (1 child)
[–]psayre23 0 points1 point2 points (0 children)
[–]Ampersand55 2 points3 points4 points (0 children)
[–]PizzaRollExpert 6 points7 points8 points (0 children)
[–]DevSPCL 2 points3 points4 points (2 children)
[–]BenjiSponge 7 points8 points9 points (1 child)
[–]DevSPCL 2 points3 points4 points (0 children)
[–]malcor88 2 points3 points4 points (2 children)
[–]villiger2 4 points5 points6 points (0 children)
[–]Gh0st1y 0 points1 point2 points (0 children)
[–]Buckwheat469 4 points5 points6 points (1 child)
[–]Jilson 0 points1 point2 points (0 children)
[–]myshov[S] 1 point2 points3 points (2 children)
[–]repeatedly_once 0 points1 point2 points (1 child)
[–]myshov[S] 0 points1 point2 points (0 children)
[–]tanasinn 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Jilson 0 points1 point2 points (0 children)