Re: An iterable DOM

Jason Orendorff:
> Arrays are iterable, because there's a new built-in
> Array.prototype.iterator method. Many DOM objects should be iterable
> too. Certainly those with indexed properties and .length:
>
>    for (var elt of document.querySelectorAll(".post:not(.grouchy)"))
>        addUnicorns(elt);
>
> Possibly also dictionaries and DOM iterators.
>
> Thoughts?

Sounds good to me, in general.  Since platform array objects and 
instances of interfaces annotated with [ArrayClass] have Array.prototype 
as their [[Prototype]], the intentionally generic 
Array.prototype.iterator should just work on them.

The interface prototype object for interfaces that support indexed 
properties should automatically get an iterator method.

Should objects with named properties automatically get an iterator 
method?  I'm not sure.

If not, or if there will there be interfaces that don't use indexed or 
named properties but which do want to expose an iterator, we could use a 
declaration like this:

   interface Bag {
     void addItem(DOMString s);
     void removeItem(DOMString s);

     DOMString iterator;
   };

which here says that the iterator will always return DOMString values.

That could also be used to override the default iteration behaviour of 
an interface with indexed properties.

I don't think dictionaries should have iterators, since they are used 
mostly by an author to pass in a bunch of named values (with a fixed set 
of names) into an IDL operation; they're just plain JS objects.

Should callback interfaces allow iterators?  I don't think so, just like 
we prevent getters/setters/etc. from being defined on them.

By "DOM iterators" above did you mean NodeIterators and TreeWalkers from 
DOM Traversal:

 
http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal.html#Traversal-NodeIterator

or interfaces like NodeList and HTMLCollection?

Received on Wednesday, 20 June 2012 06:59:39 UTC