Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Danchin
Last active October 11, 2016 06:02
Show Gist options
  • Save Aleksey-Danchin/850a0bd6185e1600ae8829e8c9fc66da to your computer and use it in GitHub Desktop.
Save Aleksey-Danchin/850a0bd6185e1600ae8829e8c9fc66da to your computer and use it in GitHub Desktop.
Делаем объекты итерируемыми для for-of по [key, value]
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