W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2011

Re: [selectors-api] Return an Array instead of a static NodeList

From: Aryeh Gregor <ayg@aryeh.name>
Date: Wed, 24 Aug 2011 15:04:54 -0400
Message-ID: <CAKA+Axm72adT9GZvqeD_FpBQBHwU1v0TaLW8_ht3Bg8dXnYKRA@mail.gmail.com>
To: Julien Richard-Foy <julien@richard-foy.fr>, Jonas Sicking <jonas@sicking.cc>
Cc: public-webapps@w3.org
On Sun, Aug 21, 2011 at 1:52 PM, Julien Richard-Foy
<julien@richard-foy.fr> wrote:
> Since Javascript 1.6, a lot of useful collection functions are defined for
> Array [1]. Unfortunately, they can’t be used directly with results returned by
> .querySelectorAll, or even .getElementsByTagName since these functions return
> NodeLists.

You can already use these methods with .call() if you want, like:
[].forEach.call(nodeList, fn).  But this is a highly unintuitive hack
-- I don't see why nodeList.forEach(fn) shouldn't work.

> I understand the DOM API is defined without a language in mind, but these
> collection functions are really useful, easy to implement and already
> available in most mainstream languages. Therefore, why not create a base
> Traversable type which would be implemented by all collection types (like
> NodeList) and which would provide the so useful bunch of iteration methods?
> Are there some issues or drawbacks I did not think of?

This sounds like a good idea.  It's not what the subject of your
e-mail says, though ("Return an Array instead of a static NodeList").
I think we should keep returning a NodeList, just make it have the
same iteration methods as an Array.

On Wed, Aug 24, 2011 at 1:27 PM, Jonas Sicking <jonas@sicking.cc> wrote:
> I agree with this, but it might be too late to make this change.
>
> The problem is that if we returned an Array object, it would not have
> a .item function, which the currently returned NodeList has.
>
> I guess we could return a Array object and add a .item function to it.

Or return a NodeList and add .forEach/.filter/etc. to it?
Received on Wednesday, 24 August 2011 19:05:43 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:47 GMT