Created
August 31, 2017 11:38
-
-
Save mislav/45781f27300c82bd7905815d232862fb to your computer and use it in GitHub Desktop.
DOM iterables on top of es6-symbol polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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