Skip to content

Instantly share code, notes, and snippets.

@allenwb allenwb/forin.js
Created May 9, 2013

Embed
What would you like to do?
The ES5 for-in requirements expressed as an ES6 generator function
function *forin(obj) {
let processed = new Set();
while (obj!==null) {
let here = Object.getOwnPropertyNames(obj);
for (let i=0; i<here.length; i++) {
let name = here[i];
if (processed.has(name)) continue;
processed.add(name);
let desc = Object.getOwnPropertyDescriptor(obj,name);
if (desc && desc.enumerable) yield name;
}
obj = Object.getPrototypeOf(obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.