NodeList是如何实现的?

发布于 2024-07-08 10:33:56 字数 334 浏览 7 评论 0原文

DOM NodeList(例如由 element.getElementsByTagName 返回)是一个有趣的对象,因为它不是快照,而是反映了创建 NodeList 后对文档所做的更改。

我想知道如何实现这样一个集合:完全惰性求值一定非常慢,但是保持缓存版本的一致性需要在内部进行大量的簿记工作。

我尝试用谷歌搜索有关该主题的博客文章,也尝试查找 Mozilla 的相关源代码文件,但无法立即找到任何内容(当我无法立即找到某些内容时,我就来到这里......)。

那么 Firefox、Safari、Internet Explorer(和其他非浏览器 DOM 实现)如何处理 NodeList?

The DOM NodeList (as returned e.g. by element.getElementsByTagName) is an interesting object, in that it is not a snapshot, but reflects changes to the document made after you have created the NodeList.

I was wondering how one would implement such a collection: Completely lazy evaluation must be horribly slow, but keeping a cached version consistent requires a lot of book-keeping internally.

I tried to google for blog articles on the subject, and also tried to find the relevant source code files for Mozilla, but could not find anything immediately (and when I cannot find something immediately, I come here...).

So how do Firefox, Safari, Internet Explorer (and other non-browser DOM implementations) handle NodeLists?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆 2024-07-15 10:33:56

对于.NET的XML库,XmlNodeList有3个具有不同策略的内部子类。 对于 XmlNode.ChildNodes 属性,XmlChildNodes 集合使用基于对其包含元素的引用的简单惰性计算。 当 DOM 更改时,XmlElementList 使用事件侦听器。 第三个,XPathNodelList,用于XPath 查询(例如,XmlNode.SelectNodes()),并在每次访问其索引、读取其Count 属性或迭代时评估XPath。

For .NET's XML library, there are 3 internal subclasses of XmlNodeList with different strategies. The XmlChildNodes collection, for the XmlNode.ChildNodes property, uses simple lazy evaluation based on a reference to its containing element. The XmlElementList uses event listeners for when the DOM changes. The third, XPathNodelList, is for XPath queries (e.g., XmlNode.SelectNodes()) and evaluates the XPath each time its index is accessed, its Count property is read, or it is iterated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文