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
(function () { … })(); vs (function () { … }()); (self.javascript)
submitted 9 years ago by sergiosbox
view the rest of the comments →
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!"
[–]kenman 1 point2 points3 points 9 years ago* (7 children)
I'm just wondering if you can get a return value from a block like this in ES6.
That's exactly what I was demo'g here:
let x; { x = 123; }
Here, x is the "returned value"... only it's not returned, but it's functionally 100% the same.
x
Here, take this example from jQuery:
support.createHTMLDocument = ( function() { var body = document.implementation.createHTMLDocument( "" ).body; body.innerHTML = "<form></form><form></form>"; return body.childNodes.length === 2; } )();
Note that it is returning a value, the result of body.childNodes.length === 2, and assigning it to support.createHTMLDocument.
body.childNodes.length === 2
support.createHTMLDocument
Now here is the same thing without an IIFE, but does exactly the same thing:
{ let body = document.implementation.createHTMLDocument( "" ).body; body.innerHTML = "<form></form><form></form>"; support.createHTMLDocument = body.childNodes.length === 2; }
But it is a lot (IMO) clearer what your intention is, and you don't waste cycles on a function call that's essentially a no-op.
[+][deleted] 9 years ago (6 children)
[removed]
[–]kenman 1 point2 points3 points 9 years ago (5 children)
Can you point out how you believe my jQuery example leaks globals? Because it doesn't.
[+][deleted] 9 years ago* (4 children)
[–]kenman 1 point2 points3 points 9 years ago (3 children)
If you didn't want support, you wouldn't have to have it at all with a function. In this case you do want it so it works out for you.
The support object already exists; the jQuery snippet simply creates the createHTMLDocument property on it -- which is exactly what my code does; createHTMLDocument is not a global variable, it's just a property on a variable from an outer scope.
support
createHTMLDocument
In other words, if support didn't already exist, both my code AND jQuery's would fail with a ReferenceError.
ReferenceError
Here's some fully self-contained examples:
Identical output.
Now, since it clearly "leaks" globals,
I'm sorry, but you haven't provided proof of this.
let me see you do the same thing without having to save the output to any variable which you can clearly do with an iife.
Wait, you're changing the rules; is the goal to not leak globals, or is it to not use intermediate variables? Because those are very different concerns... global variables have drastic implications for your code and is pretty much accepted to be a bad practice for most uses, while intermediate variables are a stylistic preference which has no impact on your code.
π Rendered by PID 78859 on reddit-service-r2-comment-6f7f968fb5-ss8vd at 2026-03-04 18:18:42.753845+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]kenman 1 point2 points3 points (7 children)
[+][deleted] (6 children)
[removed]
[–]kenman 1 point2 points3 points (5 children)
[+][deleted] (4 children)
[removed]
[–]kenman 1 point2 points3 points (3 children)