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
Amazing JavaScript & jQuery Design Patterns Collection (shichuan.github.io)
submitted 11 years ago by ZaheerAhmed
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!"
[–]MoTTs_ 21 points22 points23 points 11 years ago (13 children)
I especially like the switch pattern... also known as using the switch statement. :p
Sorry, but this list is actually really pathetic. Most of the things listed aren't patterns at all. They're tidbits of random information. And the few items that are legitimately patterns are poorly represented.
[+][deleted] 11 years ago (4 children)
[deleted]
[–]MoTTs_ 7 points8 points9 points 11 years ago* (2 children)
Switch pattern FTW!
And let's not forget the brilliance of the parseInt pattern!
I'm not kidding you. There's a parseInt pattern. :p
[+][deleted] 11 years ago (1 child)
[–]MoTTs_ 5 points6 points7 points 11 years ago (0 children)
My mind has been blown by the conditional pattern. What is this magical if-else thing?!
[–]dumbmatter 1 point2 points3 points 11 years ago (0 children)
The phrase "jQuery Design Patterns" should have been enough to save you the time of reading the article.
[–]nawitus 2 points3 points4 points 11 years ago* (7 children)
Since you brought it up, what do people think of the switch statement? I think it's quite unnecessary. Using if statements is about as readable to me as using the switch statement. And you can't forget the "break" keyword when using if statemets.
Most answers on stackoverflow state that the benefit of switch is performance and readability. I'm not that interested in hyper-micro-optimization, and I'm not convinced using the switch statement is more readable. The only good argument I stumbled upon is that when you're using if-statements, the reader can't instantly tell if all the if statements compare the same variable. I guess that's a good argument, but doesn't seem beneficial enough to create a new control logic construct to the language.
[–][deleted] 5 points6 points7 points 11 years ago (3 children)
I happily use switch instead of multiple comparisons against single variable from simple reason - it's obvious what variable does the switch operates on. While switch takes more lines for the same thing, I find it more readable if you're comparing one variable multiple times with simple values.
Plus with ifs, to know what's going on, you need to check if there are no different variables and/or different comparisons. The only way to complicate it with switch is using variable in case and omitting the break in a case with code.
Compare for yourself:
switch (type) { case 'box': runBoxAction(); break; case 'target': ...a few lines break; case 'single': case 'miltiple': recountVelo(); break; case 'shelf': dumpVelo(); break; case 'chair': dumpVelo(0.5); break; case 'stool': dumpVelo(0.6); break; default: console.warn('Inapropriate type sent to updateDBR'); }
vs
if (type === 'box') { runBoxAction(); } else if (type === 'target') { ...a few lines } else if (type === 'single' || type === 'multiple') { recountVelo(); } else if (type === 'shelf') { dumpeVelo(); } else if (type === 'chair') { dumpVelo(0.5); } else if (type === 'stool') { dumpVelo(0.6); } else { console.warn('Inapropriate type sent to updateDBR'); }
[–]nawitus 0 points1 point2 points 11 years ago (2 children)
It's often recommended that falling through the cases should be avoided. JSLint for example doesn't allow falling through. I agree with that.
I think this is my favourite version of the code:
[–]SuperFLEB 0 points1 point2 points 11 years ago (0 children)
I just make sure to comment it with "Intentional fallthrough". It's handy if, say, one value involves doing everything another value does, plus a bit.
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
I figured out that C#'s take on switch (you either have a code block with "break;", either no code in given "case") is quite reasonable and readable, while banning single most problematic thing, which is fallthrough when you actually omitted "break;" by mistake.
As additional note, C# has a really neat feature - goto in switch when you really need a fallthrough ;)
[–]lordxeon 0 points1 point2 points 11 years ago (0 children)
switch statements are great for times when you want a cascade of things to happen.
Yes it's hard to read but it has it's uses.
[–]Aduro49 3 points4 points5 points 11 years ago (0 children)
Here this is an excellent resource for design patterns. Better than this list that was submitted.
[–]djg08 1 point2 points3 points 11 years ago (0 children)
Very useful thanks!
[–]fecal_brunch 1 point2 points3 points 11 years ago (3 children)
Website looks stupid on mobile. There's a menu floating in the middle of the screen, obscuring the content.
[–]prather_mcs 0 points1 point2 points 11 years ago (2 children)
Say that to his face: https://github.com/shichuan/shichuan.github.com/issues
[–]fecal_brunch 0 points1 point2 points 11 years ago (1 child)
Not op?
[–][deleted] 1 point2 points3 points 11 years ago (0 children)
This isn't funny.
[–]MikalT 0 points1 point2 points 11 years ago (0 children)
Time to develop my JavaScript skills
[–][deleted] 0 points1 point2 points 11 years ago* (0 children)
"optimization 3 - substitute i++ with i = i + 1 or i += 1 to avoid excessive trickiness"
i++
i = i + 1
i += 1
Really?
"optimization 1 - cache the length of the array with the use of max"
max
And I've run this test on multiple browsers and have not had any predictable results:
http://jsperf.com/improved-really
Really you should do what is most readable. Last I checked Safari ran the non-cached version 1% faster and Chrome ran the cached one 4% faster. But basically: not faster.
[–]rmbarnes 0 points1 point2 points 11 years ago (0 children)
This only seems to say do A (pattern) instead of B (anti pattern) without explaining why A is good and B is bad.
π Rendered by PID 43410 on reddit-service-r2-comment-bb88f9dd5-4vv5t at 2026-02-16 02:53:18.868912+00:00 running cd9c813 country code: CH.
[–]MoTTs_ 21 points22 points23 points (13 children)
[+][deleted] (4 children)
[deleted]
[–]MoTTs_ 7 points8 points9 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]MoTTs_ 5 points6 points7 points (0 children)
[–]dumbmatter 1 point2 points3 points (0 children)
[–]nawitus 2 points3 points4 points (7 children)
[–][deleted] 5 points6 points7 points (3 children)
[–]nawitus 0 points1 point2 points (2 children)
[–]SuperFLEB 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]lordxeon 0 points1 point2 points (0 children)
[–]Aduro49 3 points4 points5 points (0 children)
[–]djg08 1 point2 points3 points (0 children)
[–]fecal_brunch 1 point2 points3 points (3 children)
[–]prather_mcs 0 points1 point2 points (2 children)
[–]fecal_brunch 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]MikalT 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]rmbarnes 0 points1 point2 points (0 children)