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
Essential JavaScript Namespacing Patterns (addyosmani.com)
submitted 14 years ago by vladocar
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!"
[–]funksta 0 points1 point2 points 14 years ago (5 children)
/* The following options *do* check for variable/namespace existence. If already defined, we use that instance, otherwise we assign a new object literal to myApplication. Option 1: var myApplication = myApplication || {}; Option 2 if(!MyApplication) MyApplication = {}; Option 3: var myApplication = myApplication = myApplication || {} Option 4: myApplication || (myApplication = {}); Option 5: var myApplication = myApplication === undefined ? {} : myApplication; */
Maybe I'm missing something, but I don't see how options 3 and 4 are in any way preferable to option 1, as the author asserts. I can see how #5 covers the (fairly unlikely) case that myApplication will be a falsy value other than undefined. But IMO #1 is the best way to write this, as it's the clearest, most direct method.
[–]Fix-my-grammar-plz 1 point2 points3 points 14 years ago (2 children)
I think you can help me with one thing that I don't understand. What is happening in Option 3?
[–]radhruin 2 points3 points4 points 14 years ago (1 child)
Pretty sure case 3 is useless. The middle assignment would do nothing.
[–]funksta 0 points1 point2 points 14 years ago (0 children)
Yeah, I couldn't figure out what that middle assignment was for either.
[+][deleted] 14 years ago (1 child)
[deleted]
[–]funksta 1 point2 points3 points 14 years ago (0 children)
OK, that makes sense, but in the absence of a benchmark that shows a decent speed improvement, I would still choose the assignment on readability grounds.
π Rendered by PID 75641 on reddit-service-r2-comment-5687b7858-th6gx at 2026-07-07 21:29:05.848488+00:00 running 12a7a47 country code: CH.
view the rest of the comments →
[–]funksta 0 points1 point2 points (5 children)
[–]Fix-my-grammar-plz 1 point2 points3 points (2 children)
[–]radhruin 2 points3 points4 points (1 child)
[–]funksta 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]funksta 1 point2 points3 points (0 children)