Skip to content Skip to sidebar Skip to footer

Why Doesn't Htmlcollection Inherit From Array

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 ar

Solution 1:


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


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 property is the only common property between them, however, contrary to Aray length, the DOM htmlCollection property is read only.

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.

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.

Post a Comment for "Why Doesn't Htmlcollection Inherit From Array"