Home:ALL Converter>How to enumerate *all* Javascript globals in a browser?

How to enumerate *all* Javascript globals in a browser?

Ask Time:2021-01-31T23:16:59         Author:rhashimoto

Json Formatter

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:

  1. Why doesn't my approach of enumerating keys work?

  2. Is there a programmatic and cross-browser way (only evergreen browsers is okay) to enumerate every member accessible through globalThis?

  3. Failing that, are there programmatic browser-specific ways to accomplish this?

Author:rhashimoto,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65980728/how-to-enumerate-all-javascript-globals-in-a-browser
yy