为什么htmlCollection不从数组继承 [英] Why doesn't htmlCollection inherit from array

查看:100
本文介绍了为什么htmlCollection不从数组继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why doesn't htmlCollection inherit from array? I understand that htmlCollection is a live collection, as opposed to an array which is a snapshot, but can't it still inherit from array?

有一种明显的< a href = https://stackoverflow.com/a/222847/4861207>将htmlCollection转换为数组,但这是对它的转换。

There's the obvious way of converting an htmlCollection to an array, but that's converting it. It's not like that by default.

更新

这与评论有关如下:


可能是 HTMLCollection An的重复项数组? – @winhowes

Possible duplicate of Is HTMLCollection An Array? – @winhowes

如评论中所述,这两个问题大相径庭。 另一个问题解释了两者之间的区别,我也在上面的问题中写道。我的问题是,为什么JavaScript的默认htmlCollection不从数组继承。

As explained in the comments, these two questions widely differ. The other question explains the difference between the two, which I also wrote in my question above. My question is, why doesn't JavaScript's default htmlCollection inherit from array.

两个问题之间的区别是,另一个问题是什么是差异,我的问题是为什么有差异。

The difference between the two questions is, the other question is 'what's the diff', my question is 'why is there a diff'.

推荐答案




问:为什么htmlCollection不从数组继承?


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

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

A: That's because htmlCollection is completely unrelated to JavaScript Arrays. The first is a DOM object created by DOM the later is created by JS Array constructor. Being unrelated to JS Array constructor, DOM htmlCollection will naturally not inherit anything from array.

htmlCollection length 属性是它们之间的唯一通用属性,但是与Aray长度相反,DOM htmlCollection属性是只读

htmlCollection length property is the only common property between them, however, contrary to Aray length, the DOM htmlCollection property is read only.

最可能使您感兴趣的部分是htmlCollection可以利用JS数组公开的大多数方法。并且可以直接分配给 HTMLCollection 原型,如您所见,该原型的构造函数:指向JS Object构造函数。

The part that most probably interests you the most, is the fact that htmlCollection can make use of most methods exposed by JS arrays. And can be directly assigned to HTMLCollection prototype, whose constructor, as you may see for yourself: points to JS Object constructor.

ps:
htmlCollection 对象到目前为止与 arguments 对象相似,而不是其他任何东西。它们都是由对象构造函数而不是数组调用的。

p.s.: The htmlCollection object is by far more similar to arguments object than anything else as they are both invoked by Object constructor, not the Array.

这篇关于为什么htmlCollection不从数组继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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