question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add [Symbol.iterator]() methods to HTMLCollection

See original GitHub issue

This is related to https://github.com/babel/babel/issues/545 and was previously filed as https://github.com/babel/babel/issues/884

I had this code (working in Firefox without Babel):

for (let contentGroupEl of xmlDocument.getElementsByTagName('contentGroups')) {
...
}

Where xmlDocument is defined as follows:

var xmlDocument = (new DOMParser).parseFromString(content, "application/xhtml+xml");

The above loop iterates over a HTMLCollection.

Babel + Browser Polyfill from babel-core npm package (v4.4.6) generates the following JS, which throws an exception in Chrome:

for (var _iterator = xmlDocument.getElementsByTagName("contentGroups")[Symbol.iterator](), _step; !(_step = _iterator.next()).done; ) {
                var contentGroupEl = _step.value;
}

TypeError: undefined is not a function: since xmlDocument.getElementsByTagName("contentGroups")[Symbol.iterator] equals undefined.

Is is possible to extend HTMLCollection with the iterator methods as well?

My current workaround looks like this:

for (let contentGroupEl of[].slice.call(xmlDocument.getElementsByTagName('contentGroups'))) {
...

Simply extending the HTMLCollection prototype does not seem to work. This code has no effect on the HTMLCollection instances.

if (!HTMLCollection.prototype[Symbol.iterator]) {
  HTMLCollection.prototype[Symbol.iterator] = [][Symbol.iterator]
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SeanHayescommented, Jan 26, 2017

I see this is marked as wontfix, but I see a linked commit that looks like it might fix this. Should iterating over HTMLCollections with for of work? It’s broken for me in Safari.

0reactions
mrichardcommented, Feb 7, 2017

@loganfsmyth when is next version of core-js expected? Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Symbol.iterator - JavaScript - MDN Web Docs
The well-known Symbol.iterator symbol specifies the default iterator for an object. ... its @@iterator method is called with no arguments, ...
Read more >
Can HTMLCollections be iterated with for...of (Symbol.iterator)?
I'm going to guess it's because of either the namedItem method on an HTMLCollection or the statement "Note: Elements is the better solution...
Read more >
Type Object must have a Symbol.iterator method that returns ...
The error "Type Object must have a Symbol.iterator method that returns an iterator" occurs when we try to use the spread syntax (...)...
Read more >
HTMLCollection forEach loop - Convert object to array
Today I'm going to show you how to make these objects a little bit more useful, by converting them into JavaScript arrays.
Read more >
TSConfig Option: downlevelIteration - TypeScript
ECMAScript 6 added several new iteration primitives: the for / of loop ( for (el of ... in ES5 environments if a Symbol.iterator...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found