It bugs me that I can’t just do document.querySelectorAll(...).map(...) even in Firefox 3.6, and I still can’t find an answer, so I thought I’d cross-post on SO the question from this blog:
http://blowery.org/2008/08/29/yay-for-queryselectorall-boo-for-staticnodelist/
Does anyone know of a technical reason why you don’t get an Array? Or why a StaticNodeList doesn’t inherit from an Array in such a way that you could use map, concat, etc?
(BTW if it’s just one function you want, you can do something like NodeList.prototype.map = Array.prototype.map;…but again, why is this functionality (intentionally?) blocked in the first place?)
I believe it to be a philosophical decision of the W3C. The design of the W3C DOM [spec] is quite orthogonal to the design of JavaScript, as the DOM is meant to be platform and language neutral.
Decisions like “
getElementsByFoo()returns an orderedNodeList” or “querySelectorAll()returns aStaticNodeList” are very much intentional, so that implementations don’t have to worry about aligning their returned data structure based on language-dependent implementations (like.mapbeing available on Arrays in JavaScript and Ruby, but not on Lists in C#).The W3C aim low: they’ll say a
NodeListshould contain a readonly.lengthproperty of type unsigned long because they believe every implementation can at least support that, but they won’t say explicitly that the[]index operator should be overloaded to support getting positional elements, because they don’t want to stymie some poor little language that comes along that wants to implementgetElementsByFoo()but cannot support operator overloading. It’s a prevalent philosophy present throughout much of the spec.John Resig has voiced a similar option as yours, to which he adds:
I do somewhat empathize. If the DOM was written specifically with JavaScript features in mind it would be a lot less awkward and more intuitive to use. At the same time I do understand the W3C’s design decisions.