[Bug 5851] Consider adding .toArray() on NodeList and HTMLCollection

http://www.w3.org/Bugs/Public/show_bug.cgi?id=5851





--- Comment #3 from Lachlan Hunt <lachlan.hunt@lachy.id.au>  2008-07-11 08:03:42 ---
(In reply to comment #1)
> 'live collections' are not a bug; they are a design feature and you could use
> that to your advantage.

Even though it's by design, some people consider it a bug because it's very
unintuitive.

> On the other hand it is already quite easy to convert such collections to an
> array in a browser that has implemented Array in a generic way so that it also
> works on interface objects:
> 
> var foo = document.getElementsByTagName('div');
> var staticFoo = [].slice.call(foo, 0);


> the toArray() could then easily be implemented by prototyping NodeList or
> HTMLCollection (don't know which one or which browsers actually support that)

It's good to know that it can be so easily provided like that for backwards
compatibility, but it's likely that a native implementation would be a bit more
efficient.

> I'd rather see a specification that says that Array methods should be generic
> and thus also work on interface objects (that can be accessed like an array
> having a length property and all) and that interface objects should be
> first-class javascript objects and thus be expandable.

This can't work because NodeLists are live (except in Selectors API).  If they
weren't live, it would have been possible to consider something like that.

Consider what it would mean for some of the mutator methods of Array to be used
directly on a NodeList. e.g.

var foo = document.getElementsByTagName('div');
foo.reverse();
foo.pop();
...

The toArray() method solves this problem, since one can easily do:

var foo = document.getElementsByTagName('div').toArray();

and not have to deal with the NodeList any more.


-- 
Configure bugmail: http://www.w3.org/Bugs/Public/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

Received on Friday, 11 July 2008 08:04:16 UTC