Created
July 18, 2014 00:05
-
-
Save allenwb/b185830a0a3f9f7b418e to your computer and use it in GitHub Desktop.
[[Enumerate]] in JS code
This file contains 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
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