I'm looking for a programmatic way to get a list of browser globals, including built-in constructors like Array
, Object
, etc.
If I type Javascript in the Chrome console, I get this somewhat odd behavior:
console.log('Array' in globalThis); // true
const s = new Set(function*() { for (const key in globalThis) yield key; }());
console.log(s.has('Array')); // false
That is, I can check if the Array
constructor is in the global object, but when I enumerate its members the Array
constructor is not there.
My questions:
Why doesn't my approach of enumerating keys work?
Is there a programmatic and cross-browser way (only evergreen browsers is okay) to enumerate every member accessible through globalThis
?
Failing that, are there programmatic browser-specific ways to accomplish this?
@NiettheDarkAbsol is correct in the comments that not all properties are enumerable. It turns out to be possible to enumerate "non-enumerable" properties anyway though, by using Object.getOwnPropertyNames()
(and Object.getOwnPropertySymbols()
if you need them) to get direct properties. If you need inherited properties as well, you'll need to do this over the prototype chain.