Created
November 15, 2017 18:43
-
-
Save bbaaxx/a781e2959841770aee6f90abf1010212 to your computer and use it in GitHub Desktop.
Code for blog post about iterators iterables and generators with ES6
This file contains hidden or 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
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