简体   繁体   English

为什么htmlCollection不从数组继承

[英]Why doesn't htmlCollection inherit from array

Why doesn't htmlCollection inherit from array? 为什么htmlCollection不从数组继承? 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? 我知道htmlCollection是一个实时集合,而不是一个快照的数组,但是它还不能继承数组吗?

There's the obvious way of converting an htmlCollection to an array , but that's converting it. 有一种将htmlCollection转换为数组的明显方法,但是它正在转换它。 It's not like that by default. 默认情况下不是那样的。

Update 更新

This is regarding the comment below: 这是关于以下评论:

Possible duplicate of Is HTMLCollection An Array? 可能重复的是HTMLCollection一个数组? – @winhowes - @winhowes

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. 我的问题是,为什么JavaScript的默认htmlCollection不从数组继承。

The difference between the two questions is, the other question is 'what's the diff', my question is 'why is there a diff'. 这两个问题的区别在于, 另一个问题是“差异是什么”,我的问题是“为什么会出现差异”。


Q: "Why doesn't htmlCollection inherit from array?" 问: “为什么htmlCollection不从数组继承?”


A: That's because htmlCollection is completely unrelated to JavaScript Arrays. 答:那是因为htmlCollection与JavaScript Arrays完全无关。 The first is a DOM object created by DOM the later is created by JS Array constructor. 第一个是由DOM创建的DOM对象,后者是由JS Array构造函数创建的。 Being unrelated to JS Array constructor, DOM htmlCollection will naturally not inherit anything from array. 与JS Array构造函数无关,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 length属性是它们之间唯一的公共属性,但是,与Aray长度相反,DOM htmlCollection属性是只读的

The part that most probably interests you the most, is the fact that htmlCollection can make use of most methods exposed by JS arrays. 最可能让您感兴趣的部分是,htmlCollection可以使用JS数组公开的大多数方法。 And can be directly assigned to HTMLCollection prototype, whose constructor , as you may see for yourself: points to JS Object constructor. 并且可以直接分配给HTMLCollection原型,其构造函数 ,如您自己可以看到的:指向JS Object构造函数。

ps: 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. ps: htmlCollection对象与arguments对象的相似之处远远超过其他任何对象,因为它们都是由Object构造函数调用的,而不是Array。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM