So, originally, I thought that the limitation was that Object.keys
enumerates only an object's own properties and not inherited ones. However, trying a simple for...in
loop without a hasOwnProperty
check failed to yield much more than Object.keys
. I also tried looping over globalThis
to no avail.
A bit of further research showed that JSON
is part of the family of "Standard Built-In Objects". When researching that, I came upon a similar Stack Overflow question regarding how to list all standard built-in objects in Node, and the solution was to leverage getOwnPropertyNames
. This proved to be the solution-- rather than getting ≈300 results, I instead got 1000+, among which were "JSON"
and "Error"
.
const allWindowProperties = Object.getOwnPropertyNames(window);
console.log(allWindowProperties);
console.log(allWindowProperties.includes('JSON'));
console.log(allWindowProperties.includes('Error'));
So hopefully this solves your problem. My original comment, however, is still worth exploring: How has this use case manifested? Why is it a necessity to allow the user direct access to the runtime environment to declare variables at all, let alone in the global namespace? Perhaps you have a good reason, and I do think this is an interesting question, but I wonder if perhaps we have a classic X-Y problem here.