为什么 document.querySelectorAll 返回的是 StaticNodeList 而不是真正的 Array? [英] Why does document.querySelectorAll return a StaticNodeList rather than a real Array?

查看:20
本文介绍了为什么 document.querySelectorAll 返回的是 StaticNodeList 而不是真正的 Array?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我感到困扰的是,即使在 Firefox 3.6 中我也不能只执行 document.querySelectorAll(...).map(...) ,但我仍然找不到答案,所以我想我会在这个博客上交叉发布这个问题:

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/

有没有人知道您没有获得 Array 的技术原因?或者为什么 StaticNodeList 不能以您可以使用 mapconcat 等的方式从 Array 继承?

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?

(顺便说一句,如果它只是您想要的一个功能,您可以执行类似 NodeList.prototype.map = Array.prototype.map; 之类的操作......但同样,为什么这个功能(故意?) 一开始就被屏蔽了?)

(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?)

推荐答案

我相信这是 W3C 的一个哲学决定.W3C DOM [规范] 的设计与 JavaScript 的设计完全正交,因为 DOM 意味着是平台和语言中立的.

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.

诸如getElementsByFoo() 返回一个有序的 NodeList"或querySelectorAll() 返回一个 StaticNodeList 之类的决定"" 是非常有目的的,因此实现不必担心根据依赖于语言的实现来对齐返回的数据结构(例如 .map 在 JavaScript 和 Ruby 中的数组上可用,但是 在 C# 中的列表上).

Decisions like "getElementsByFoo() returns an ordered NodeList" or "querySelectorAll() returns a StaticNodeList" are very much intentional, so that implementations don't have to worry about aligning their returned data structure based on language-dependent implementations (like .map being available on Arrays in JavaScript and Ruby, but not on Lists in C#).

W3C 的目标很低:他们会说 NodeList 应该包含 只读 .length 类型为 unsigned long 的属性,因为他们相信每个实现都至少可以支持 那个,但他们不会明确说应该重载 [] 索引运算符以支持获取位置元素,因为他们不想阻碍一些想要实现 getElementsByFoo() 但不支持运算符重载.这是贯穿大部分规范的普遍理念.

The W3C aim low: they'll say a NodeList should contain a readonly .length property 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 implement getElementsByFoo() but cannot support operator overloading. It's a prevalent philosophy present throughout much of the spec.

John Resig 表达了与您类似的选项他补充道:

John Resig has voiced a similar option as yours, to which he adds:

我的论点并非如此NodeIterator 不是很像 DOM它不是很像 JavaScript.它没有利用这些功能存在于 JavaScript 语言中,并且尽其所能使用它们...

My argument isn't so much that NodeIterator isn't very DOM-like it's that it isn't very JavaScript-like. It doesn't take advantage of the features present in the JavaScript language and use them to the best of its ability...

我确实有点同情.如果 DOM 是专门为 JavaScript 特性编写的,那么使用起来会不会那么尴尬并且更直观.同时我也理解 W3C 的设计决策.

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.

这篇关于为什么 document.querySelectorAll 返回的是 StaticNodeList 而不是真正的 Array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆