None of these answers explain why NodeList does not inherit from Array, which allows it to have forEach and everyone else.
The answer is found in this thread discussion . In short, it breaks the Internet:
The problem was that the incorrectly assumed instance was to mean that the instance was an array in combination with Array.prototype.concat.
An error has occurred in the Google Closure Library, due to which almost all Google applications have failed. The library was updated as soon as it was discovered, but there might still be code that makes the same wrong assumption when combined with concat.
That is, some code did something like
if (x instanceof Array) { otherArray.concat(x); } else { doSomethingElseWith(x); }
However, concat will process "real" arrays (and not instanceof Array) differently than other objects:
[1, 2, 3].concat([4, 5, 6])
so that means the above code broke when x was a NodeList, because before it went down the doSomethingElseWith(x) path, then after that it went along the otherArray.concat(x) path, which did something- that's weird since x wasn 't a real array.
For some time there has been a proposal for the Elements class, which was a real subclass of Array and was used as the "new NodeList". However, this has been removed from the DOM standard , at least for the time being, since it is not yet possible to implement many technical and technical requirements related reasons.
Domenic Nov 19 '14 at 18:25 2014-11-19 18:25
source share