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.
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 > 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'.
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.
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.
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?