Skip to content

Instantly share code, notes, and snippets.

@allenwb
Created July 18, 2014 00:05
Show Gist options
  • Save allenwb/b185830a0a3f9f7b418e to your computer and use it in GitHub Desktop.
Save allenwb/b185830a0a3f9f7b418e to your computer and use it in GitHub Desktop.
[[Enumerate]] in JS code
function *enumerate(obj) {
if (Object(obj)!==obj) return undefined;
let visited = new Set;
while (obj!==null ) {
for (name of Object.getOwnPropertyNames(obj)) {
if (!visited.has(name)) {
let desc = Object.getOwnPropertyDescriptor(obj,name);
if (desc) {
visited.add(name);
if (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