Skip to content

Instantly share code, notes, and snippets.

@mislav
Created August 31, 2017 11:38
Show Gist options
  • Save mislav/45781f27300c82bd7905815d232862fb to your computer and use it in GitHub Desktop.
Save mislav/45781f27300c82bd7905815d232862fb to your computer and use it in GitHub Desktop.
DOM iterables on top of es6-symbol polyfill
function iterator() {
const self = this
let i = 0
return {
next: function() {
return {
done: i === self.length,
value: self[i++]
}
}
}
}
if (!Array.prototype[Symbol.iterator]) {
Array.prototype[Symbol.iterator] = iterator
}
if (!NodeList.prototype[Symbol.iterator]) {
// IE11 refuses to assign directly onto NodeList.prototype
Object.defineProperty(NodeList.prototype, Symbol.iterator, {
enumerable: false,
configurable: true,
get: function() { return iterator }
})
}
if (!HTMLCollection.prototype[Symbol.iterator]) {
// IE11 refuses to assign directly onto HTMLCollection.prototype
Object.defineProperty(HTMLCollection.prototype, Symbol.iterator, {
enumerable: false,
configurable: true,
get: function() { return iterator }
})
}
if (window.Headers && !Headers.prototype[Symbol.iterator]) {
Headers.prototype[Symbol.iterator] = function() {
const items = []
this.forEach(function(value, name) {
items.push([name, value])
})
return items[Symbol.iterator]()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment