Bug 17296 - forEach does not work with document.querySelectorAll
Summary: forEach does not work with document.querySelectorAll
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore JavaScript (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac OS X 10.5
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-02-10 15:07 PST by Richard York
Modified: 2024-03-17 09:30 PDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard York 2008-02-10 15:07:46 PST
forEach does not work with document.querySelectorAll().

The following,

document.querySelectorAll('body').forEach(
  function ($node) {

  }
);

Throws:

Value undefined (result of expression document.querySelectorAll("body").forEach) is not object.

Tried this today in the latest nightly.
Comment 1 Sam Weinig 2008-02-10 15:14:57 PST
This is not expected to work as querySelectorAll returns a StaticNodeList, not an Array.  StaticNodeList does not have a forEach method.  A work around would be to use Array.prototype.forEach.call.

Richard, why did you expect this to work?  Does it work in other implementations?  Should we consider adding it?
Comment 2 Richard York 2008-02-10 15:35:30 PST
Thanks for explaining that.

It's a pretty common thing to do with JavaScript frameworks.  Makes it easier to enumerate over elements.

I use Dean Edwards's Base2... which allows this kind of traversal, and it seems like I've seen similar code in use with other more popular frameworks, but I'm not sure about that.

Dean shows how document.querySelectorAll can be used with forEach here:
http://dean.edwards.name/weblog/2007/03/yet-another/

Though, reading that again, he made that distinction clear too, so I understand why it isn't expected to work, but I think it'd be pretty useful.  I use that all over the place in my apps, makes the code more readable and compact.
Comment 3 Anne van Kesteren 2024-03-17 09:30:05 PDT
Unfortunately adding this to NodeList has proven difficult. Happy to reconsider if it gets standardized somehow.