Skip to content

Instantly share code, notes, and snippets.

@bbaaxx
Created November 15, 2017 18:43
Show Gist options
  • Save bbaaxx/a781e2959841770aee6f90abf1010212 to your computer and use it in GitHub Desktop.
Save bbaaxx/a781e2959841770aee6f90abf1010212 to your computer and use it in GitHub Desktop.
Code for blog post about iterators iterables and generators with ES6
const someObject = {
a: 1,
perrito: 'Yolodog',
aChar: 'a',
}
someObject[Symbol.iterator] = function() {
let currentKey = 0;
const keys = Object.keys(this);
const { length } = keys;
return {
next: function() {
return currentKey < length
? { value: this[keys[currentKey++]], done: false }
: { done: true };
}.bind(this),
};
};
for (let item of someObject) { console.log(item); }
// 1
// Yolodog
// a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment