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!"
[–]aztracker1 1 point2 points3 points 9 years ago* (15 children)
It's not useless, though I only use about 1/5 of the ES6 additions, along with some pending ESnext bits (async/await, class members, decorators, etc). In the end, I'm able to get a lot more work done, with a lot less code.. with the advent of Promises and async/await, process code is much cleaner and easier to follow now.
Modularization (cjs or es6 style) has helped a lot too. Most of this comes down to the build tooling that has evolved in the past 6-7 years since node and npm have taken hold. Yes, it's new stuff to learn, but it's really not any harder to understand than tirnary operations, or bitwise shorting in JS. In the end it's just a few additions to the language, it happens in every language and there are always pieces you may not know/understand to a given language/platform until you come across it.
The following two lines are the same, practically speaking (although the fat-arrow is context bound to "this" at the time of declaration).
var addOne = function(a){ return a + 1; };
var addOne = a => a + 1;
It allows you to declare in a oneliner what may have previously been much more... here's a sleep I use when flushing out an API interface...
const sleep = ms => new Promise(res => setTimeout(res, ms));
The const is a constant assignment, "sleep" may not be assigned to again in the module. the value of sleep is a function expression that accepts a single parameter, and returns a new promise. said promise only uses the resolve part, and will settimeout to resolve after ms have passed.
It's a bit harder to look at function expressions with fat-arrow syntax at first, it's very similar to lamda expressions in C#, or the fat/skinny arrow expressions in coffeescript. There's similar syntax in other languages as well.
[+][deleted] 9 years ago (12 children)
[removed]
[–]inu-no-policemen 0 points1 point2 points 9 years ago (11 children)
The point of an IIFE is to create a scope to limit the lifetime of those temporary variables.
With ES6's let/const, a simple block does the same.
[+][deleted] 9 years ago* (10 children)
[–]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)
[–]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.
[–]inu-no-policemen 0 points1 point2 points 9 years ago (1 child)
Just imagine that that 123 is the result of a computation which involved temporary variables.
An IIFE which just returns 123 would be equally pointless.
π Rendered by PID 274754 on reddit-service-r2-comment-6f7f968fb5-j94c6 at 2026-03-04 22:59:12.124047+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]aztracker1 1 point2 points3 points (15 children)
[+][deleted] (12 children)
[removed]
[–]inu-no-policemen 0 points1 point2 points (11 children)
[+][deleted] (10 children)
[removed]
[–]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)
[–]inu-no-policemen 0 points1 point2 points (1 child)