I'm having a hard time converting a NodeList
to an array in IE 8. The following works perfectly in Chrome, but in IE 8 toArray()
is not recognized as valid:
NodeList.prototype.toArray = function() {
var a = [];
for (var i = 0, len = this.length; i < len; i++) {
a[i] = this[i];
}
return a;
}
document.all.tags("div").toArray();
I tried adding a prototype function to an array just to check my sanity and it works correctly. That makes me think IE 8 doesn't actually return a NodeList
? Here's a full example:
What am I doing wrong?
NodeList
has to be a visible and alterable constructor function, or that if there is a constructor function visible asNodeList
that it will be used as the return type of all NodeList-returning methods. (After all, achildNodes
NodeList and agetElementsByTagName
NodeList do very different things.) Prototyping onto the native JS objects is specified by the ECMAScript standard and is reliable; prototyping onto DOM Nodes and other objects not defined by the language standard is unreliable and should be avoided. – Downandout