Last active
October 11, 2016 06:02
-
-
Save Aleksey-Danchin/850a0bd6185e1600ae8829e8c9fc66da to your computer and use it in GitHub Desktop.
Делаем объекты итерируемыми для for-of по [key, value]
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 object = {a: 1, b: 2}; | |
Object.defineProperty(Object.prototype, Symbol.iterator, { | |
enumirable: false | |
, configurable: true | |
, get: () => function * () { | |
for (const key of Object.keys(this)) | |
yield [key, this[key]]; | |
} | |
}); | |
for (const [key, value] of object) | |
console.log(key, value); | |
// a 1 | |
// b 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment