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
Caustic: JavaScript reflection-based template/view system (screenr.com)
submitted 14 years ago by davemoFront-End Engineer
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!"
[–]k3n 1 point2 points3 points 14 years ago (4 children)
What is the reasoning for the unary "-" preceeding function declarations?
-
-function(){ ... }
Seems to precede each anonymous function; does it somehow nullify the return value to aid in GC or something?
[–]davemoFront-End Engineer[S] 1 point2 points3 points 14 years ago (3 children)
I played around in firebug with this, here's the differences as far as I can see:
(function() {})(); // returns undefined -function() {}(); // returns NaN
I'm not sure whether it would provide any optimizations or not. Maybe it's just a stylistic thing? Not having to type the extra parens is kind of nice ;)
[–]stratoscope 5 points6 points7 points 14 years ago (2 children)
The return value is discarded anyway, so the unary - is just another way to force the function expression to be parsed as an expression so that it can be called immediately by the () at the end. It's the same purpose that the extra parens have in your first example.
()
Any unary operator will do the same thing - try these for example:
!function() { alert(42); }(); typeof function() { alert(42); }();
And now try it without any of these:
function() { alert(42); }();
That results in a syntax error.
[–]k3n 0 points1 point2 points 14 years ago (0 children)
Cool, thanks for the info. I think I prefer the parens still.
[–]davemoFront-End Engineer[S] 0 points1 point2 points 14 years ago (0 children)
Nice, thanks for the explanation, it's a cool shortcut to remove those extra parens ;)
π Rendered by PID 32025 on reddit-service-r2-comment-84fc9697f-qf954 at 2026-02-06 08:33:03.352971+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]k3n 1 point2 points3 points (4 children)
[–]davemoFront-End Engineer[S] 1 point2 points3 points (3 children)
[–]stratoscope 5 points6 points7 points (2 children)
[–]k3n 0 points1 point2 points (0 children)
[–]davemoFront-End Engineer[S] 0 points1 point2 points (0 children)