Skip to content

Instantly share code, notes, and snippets.

@allenwb
Created May 9, 2013 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenwb/5548164 to your computer and use it in GitHub Desktop.
Save allenwb/5548164 to your computer and use it in GitHub Desktop.
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