window.blah === undefined
and require the more verbose equivalent I've seen pushed on c.l.j:
typeof window.blah === "undefined"
There's simply no market left for this sort of paleo-Javascript.
if (undefined = someVar) {...}
The typeof check cannot be broken in this way.
15.1.1.3 undefined The value of undefined is undefined (see 8.1). This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
It's true that it can be broken today by unintentional assignment at the global scope, but you're basically skating on thin ice as soon as you redefine undefined or one of the many built-in global object types or global mutable values anyways (Array, Element, Object, NaN for example). Ditto for adding anything to Object.prototype.
I wouldn't blame a framework for wanting to place at least some burden on the user to follow a set of basic guidelines to ensure the library functions correctly. Ideally you'd ship your development-time, unminified version of the library with a bunch of startup-time assertions to ensure that the user isn't accidentally walking over core JS objects.
Alternatively, you could workaround the mutable nature of some of the globals by defining your own in-scope. When these values are eventually made read-only, these assignments should become no-ops:
var undefined = void 0; var NaN = 0/0; var Infinity = 1/0;
This is comment is rambling a bit, but I think I can justify the position that at least some of the recommended practices of the c.l.j folk are wordy, unnecessary and out-dated.
I think he just has angry-hyperactively-perfectionist-armchair-developer syndrome.
Case in point: where is his brilliant contribution to the JavaScript ecosystem?
The scary thing is that I don't think he actually is a troll. I could be wrong, though.
The bad side: he consistently antagonises people, has what seems to be a personal vendetta against jQuery and declines to publish useful, constructive critiques, instead posting ill-formatted, snide rants dressed up as code reviews on comp.lang.javascript. His behaviour towards John Resig has resulted in Resig claiming to have turned his back entirely on comp.lang.javascript, which is great shame because I think there is a lot he could learn there to improve jQuery. If David Mark's attacks are turning people away from comp.lang.javascript, then that is unfortunate because he is by no means the only expert on there and others experts on there will have less of an audience for their insightful posts.
I think it's a bold claim to say that David's understanding of the DOM is "significantly deeper" than John's. Especially considering that the DOM is not a particularly complex set of APIs.
One can only hope that he does actually improve Dojo, instead of destroy their community or credibility with his rants.
Not really most libraries use feature testing, especially jQuery and John advocates feature testing in quite a few of his writings.
Instead of detecting which browser you are using based on which features you support, the script should check to see if the features it wants to use exist, and if so use them.
So, instead of:
var isIE = !!document.all;
function doSomething()
{
if (isIE) document.all.whatever();
}
You do: if (document.all)
{
function doSomething()
{
document.all.whatever();
}
}
Even closer to the point, he means that these are both bad form: var isIE = navigator.userAgent.ssubstr...;
var isIE = !!document.all;However, I have a challenge for you: calculate the browser window's width in pixels, before the page is done loading, and return the correct value without doing any browser sniffing.
Enjoy. :-)
There is some shred of validity to them.
However, most JS coders are too busy using jQuery or Prototype or something to Get Their Project Done and then move on to the next thing to be bothered by this guy.