TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

JavaScript pattern and antipattern collection

98 pointsby mshafrirabout 13 years ago

8 comments

StefanKarpinskiabout 13 years ago
You know what's an antipattern? Putting a fixed floating div <i>over</i> the text you're supposed to be reading so that you can't read it. Brilliant.
评论 #3759598 未加载
romanivabout 13 years ago
This is a great example of why I dislike JS as a language. There is a page on optimizing for loops with <i>6 examples</i>. Four of them are deemed sub-optimal. The two "preferred" examples are miles away from being intuitive or natural. And it's not an exception.<p>There are just way too many ways to do things wrong.
评论 #3759049 未加载
greggmanabout 13 years ago
Everyone has their own style. As one controversial example, some programmers do this<p><pre><code> if (condition) singleStatement; </code></pre> Their POV is less lines = better.<p>And others do this<p><pre><code> if (condition) { singleStatement; } </code></pre> Their POV is defensive programming and or consistency is better.<p>Similarly, some programmers prefer<p><pre><code> var a, b, c = 3; </code></pre> Because less typing = better. Others prefer<p><pre><code> var a; var b; var c = 3; </code></pre> Because they prefer consistency and defensiveness. Deleting any line doesn't effect the others where as in the 1st example, delete the first or the last line and other lines have to change (which is funny since that means MORE typing).<p>Yet another example<p><pre><code> if (condition) { statement; statement; } </code></pre> vs<p><pre><code> if (condition) { statement; statement; } </code></pre> Some prefer the first because it's shorter. Others prefer the second because it's consistent and keeps the blocks separate and movable.<p>IMO one vs the other is not a pattern vs anti-pattern. It's a style decision. Me, I'm on the consistency and defensive programming &#62; shortness of typing side of this fence. It's disheartening to see some self appointed JS gurus claim their 'style' is better when clearly it's just 'style'.
评论 #3759606 未加载
sixcornersabout 13 years ago
We have to name javascript functions twice?<p>We should be using eval to get the global object?<p>If you are going to go through that hassle to get the global object why not do the same for everything else you will ever use? Every single external variable could have it's own privately named identifier. Won't that be fun.
评论 #3759247 未加载
评论 #3758028 未加载
geuisabout 13 years ago
Could you make it so the sidebar doesn't cover 40% of the screen and thus most of the content?
评论 #3758194 未加载
omgtehlionabout 13 years ago
some of these “antipatterns” are just stupidities. and a little number of “good practices” in this list actually have a lot of downsides, rendering them antipatterns.<p>and the sidebar is really annoying.
iamleppertabout 13 years ago
I've never understood:<p>if(typeof document.attachEvent !== undefined) document.attachEvent(...)<p>wouldn't if(typeof document.attachEvent == 'function') be much better?<p>that way you assure it's actually something that's actually callable?
评论 #3759323 未加载
ww520about 13 years ago
One man's anti-pattern is another man's normal coding. Just don't be dogmatic about it.