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

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

问题描述

让我烦恼的是,即使在 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/

有没有人知道为什么你没有得到一个数组的技术原因?或者为什么一个 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 应该包含一个 readonly .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 而不是一个真正的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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