Bug 17726 - Enumerables on lists
Summary: Enumerables on lists
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: PC All
: P2 enhancement
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
Depends on:
Reported: 2012-07-09 20:23 UTC by Jonathan Neal
Modified: 2012-11-21 18:58 UTC (History)
5 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Neal 2012-07-09 20:23:45 UTC
Should NodeList, DOMTokenLists, and other list enumerables behave like Array enumerables?

Right now, properties like "length", "item", "add", "remove", "toggle", and "contains" are enumerable, which makes for (e in someList) not as zesty as for (e in someArray).
Comment 1 Erik Arvidsson 2012-07-09 21:28:52 UTC
It is considered bad practice to use for-in loops for arrays and array like objects.

FWIW, "length" is non enumerable in IE and Firefox. It is enumerable in WebKit/JSC, WebKit/V8 and Opera.

ES6 solves this using the for-of loop wich uses an iteration hook. http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts. The right thing to do is probably to spec that the default iterator of these things should be that same as Array.prototype.iterator. which would allow us to do:"

for (var element of document.querySelectorAll('p')) {
  assert(element.tagName === 'P');
Comment 2 Boris Zbarsky 2012-07-10 06:45:34 UTC
Jonathan, "for (e in someArray)" is pretty broken, since it will enumerate all sorts of things in various cases.  Just don't go there.  ;)

Gecko+Spidermonkey already has support for for-of on lists.  And there's discussion about making that automatic in WebIDL.
Comment 3 Anne 2012-11-21 18:58:09 UTC
Jonathan, our plan of dealing with this is bug 17648. Hopefully that is okay with you. Please let me know if you have any concerns.