I understand that HTMLCollection is not actually an array, or else it would be defined as an array. I use a help function that I call isArray() to detect whether or not an object is an array. I use this little helper all over the place and i’ve been running problems on this returning false when checking against an htmlCollection.
var isArray: function(obj) {
var type = Function.prototype.call.bind( Object.prototype.toString );
return type(obj) === '[object Array]' || type(obj) === '[object HTMLCollection]';
}
Would it be wrong to check for the htmlCollection type within this helper function and assume that it is the same thing as an array? What makes it any different? Other than its html elements as opposed to javascript objects.
No, it’s an
HTMLCollection, not anArray.It has Array-like characteristics, like numeric properties, and a
.lengthproperty, but it does not inherit fromArray.prototype. Therefore it does not have the standardArraymethods, and so should be seen as being different.Another significant difference is that
HTMLCollectionis a “live” collection, which means that it updates as the DOM updates. If you remove one of its nodes from the DOM, it will be removed from theHTMLCollectionautomatically.