为什么htmlCollection不从数组继承

Why doesn't htmlCollection inherit from array

为什么htmlCollection不继承数组?我知道 htmlCollection 是一个实时集合,而不是一个快照数组,但它不能仍然从数组继承吗?

converting an htmlCollection to an array 有明显的方法,但那是转换它。默认情况下不是这样的。

更新

这是关于下面的评论:

Possible duplicate of Is HTMLCollection An Array? – @winhowes

如评论中所述,这两个问题大不相同。 The other question解释了两者的区别,我在上面的问题中也写了。我的问题是,为什么 JavaScript 的默认 htmlCollection 不继承自数组。

两个问题的区别是,the other question是'有什么不同',我的问题是'why is there a diff'。


问: "Why doesn't htmlCollection inherit from array?"


A: 那是因为 htmlCollection 与 JavaScript 数组完全无关。 第一个是由 DOM 创建的 DOM 对象,后者是由 JS Array 构造函数创建的。与 JS Array 构造函数无关,DOM htmlCollection 自然不会从 array 继承任何东西。

htmlCollection length 属性 是它们之间唯一共同的属性,然而,与数组长度相反,DOM htmlCollection 属性 是 只读

您最感兴趣的部分可能是 htmlCollection 可以使用 JS 数组公开的大多数方法。并且可以直接赋值给HTMLCollection原型,其constructor,自己看吧:分到 JS 对象构造函数。

p.s.: htmlCollection 对象与 arguments 对象比其他任何对象都更相似,因为它们都是由对象构造函数调用的,而不是数组。