Garrett Smith wrote:
 >>> It would be great if NodeLists were subclasses of JavaScript Array

It wouldn't really make sense to do that.

agreed, that would be horirble

You can assign the Array methods to the NodeList prototype, they're
generic.

I'm going to take slice as an example here - lets say you wanted the first n nodes in a NodeList (no, I don't see how that would be useful, but lets pretend that it is).

Was there any thought that went into that statement?

This is horrible advice for several reasons:

1) Modifying host objects with new properties is a good way to create
bugs and confuse developers.
Would you rather have another implementation written in ECMAScript? Or Array.prototype.slice.call(nodelist, 0, n);

2) NodeList is an interface and should not have any implementation.
Even in browsers that expose a NodeList object, it cannot be
guaranteed
WebIDL is here to specify the behavior. In ECMAScript there is no such thing as an interface, so WebIDL maps them to what it refers to as "interface objects" and "interface prototype objects". It would seem reasonable that you cannot call functions on these objects directly, or that functions could appear further down the prototype chain than is implied by the IDL (as long as the function appears on all the relevant DOM objects). In any case adding a new function should work if WebIDL has been followed.

3) You answered a question for which no need was demonstrated, and
provided no example.
I could argue that there is no need for interactive web pages at all (they tend to annoy me), but we want to be flexible.

4) Calling any of the array methods on a NodeList would obviously
fail. We can take a look at push, for example:-

javascript:try{ alert(Array.prototype.push.call(document.childNodes));
} catch(ex){alert(ex.message);}

Should err out in step 7 of push attempting to set the "length" property.
I believe length is read-only on a NodeList, and assigning to read-only members in ECMAScript fails silently. So we should carry on regardless.

Modifying host objects is a very bad idea. NodeList is an Interface.
An interface should have no implementation. Even if you really wanted
to follow Ian's advice, it wouldn't work.


OK, I'll agree that in general its a bad idea, and all hell breaks loose if you have multiple windows. But it should be specified in WebIDL, and if the document implies that it should work (which it does) and it is not a requirement for it to work, then it should say so (and preferably explain why).

Reply via email to