4 ms·
HTMLCollection is a messed-up puppy, sadly. Consider this markup: <img name="forEach"> <script> document.images.forEach.src = "something";
by bzbarsky 12y ago
HTMLCollection is a messed-up puppy, sadly. Consider this markup:
<img name="forEach">
<script>
document.images.forEach.src = "something";
</script>
This works, today, in all browsers. Adding "forEach" to the proto chain of HTMLCollection would make this code no longer work, so no browser is willing to add it for fear of breaking sites.
The answer is to stop using HTMLCollection.
What's worse is that NodeList doesn't have this named-lookup issue, but putting Array.prototype on its proto chain (the obvious simplest way to get all the array goodies) again breaks websites, because some people uses "instanceof Array" to assume things about how an object will interact with Array.prototype.concat.
Long story short, making improvements to the DOM is very hard because of the two decades of weird website hacks that are supposed to not get broken in the process...