为什么 nodelist 没有 forEach? [英] Why doesn't nodelist have forEach?

查看:34
本文介绍了为什么 nodelist 没有 forEach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简短的脚本来更改 元素的内部文本,但发现 nodelist 没有 forEach 方法.我知道 nodelist 不是从 Array 继承的,但是 forEach 似乎不是一个有用的方法吗?是否有我不知道的特定实现问题会阻止将 forEach 添加到 nodelist?

I was working on a short script to change <abbr> elements' inner text, but found that nodelist does not have a forEach method. I know that nodelist doesn't inherit from Array, but doesn't it seem like forEach would be a useful method to have? Is there a particular implementation issue I am not aware of that prevents adding forEach to nodelist?

注意:我知道 Dojo 和 jQuery 都有某种形式的 forEach 用于它们的节点列表.由于限制,我无法使用.

Note: I am aware that Dojo and jQuery both have forEach in some form for their nodelists. I cannot use either due to limitations.

推荐答案

NodeList 现在在所有主流浏览器中都有 forEach()

请参阅 MDN 上的 nodeList forEach().

这些答案都没有解释为什么 NodeList 不继承自 Array,从而允许它具有 forEach 和所有其他的.

None of these answers explain why NodeList doesn't inherit from Array, thus allowing it to have forEach and all the rest.

找到答案在这个 es-discuss 线程上.简而言之,它破坏了网络:

The answer is found on this es-discuss thread. In short, it breaks the web:

问题是代码错误地假定 instanceof 意味着该实例是一个与 Array.prototype.concat 结合的数组.

The problem was code that incorrectly assumed instanceof to mean that the instance was an Array in combination with Array.prototype.concat.

Google 的 Closure Library 中存在一个错误,导致几乎所有 Google 的应用程序都因此而失败.该库在发现后立即更新,但可能仍有代码在与 concat 结合使用时做出相同的错误假设.

There was a bug in Google's Closure Library which caused almost all Google's apps to fail due to this. The library was updated as soon as this was found but there might still be code out there that makes the same incorrect assumption in combination with concat.

也就是说,一些代码做了类似的事情

That is, some code did something like

if (x instanceof Array) {
  otherArray.concat(x);
} else {
  doSomethingElseWith(x);
}

然而,concat 将处理真实的"数组(不是 instanceof Array)与其他对象不同:

However, concat will treat "real" arrays (not instanceof Array) differently from other objects:

[1, 2, 3].concat([4, 5, 6]) // [1, 2, 3, 4, 5, 6]
[1, 2, 3].concat(4) // [1, 2, 3, 4]

所以这意味着当 x 是一个 NodeList 时,上面的代码被破坏了,因为在它沿着 doSomethingElseWith(x) 路径之前,而之后它沿着 otherArray.concat(x) 路径,它做了一些奇怪的事情,因为 x 不是一个真正的数组.

so that means that the above code broke when x was a NodeList, because before it went down the doSomethingElseWith(x) path, whereas afterward it went down the otherArray.concat(x) path, which did something weird since x wasn't a real array.

一段时间以来,有人提议将 Elements 类作为 Array 的真正子类,并将用作新的 NodeList".然而,这是从DOM标准中删除,至少现在是这样,因为它不是'由于各种技术和规范相关的原因,尚不可行.

For some time there was a proposal for an Elements class that was a real subclass of Array, and would be used as "the new NodeList". However, that was removed from the DOM Standard, at least for now, since it wasn't feasible to implement yet for a variety of technical and specification-related reasons.

这篇关于为什么 nodelist 没有 forEach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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