let globalThis = undefined
gives me no error in browsers
let [global, globalThis] = [undefined, undefined]
gives me no error in node
However let window = undefined
throws SyntaxError
window
must be present for the page(frame) to work. Since window
is declared in a global execution context, you can't redeclare it with let
or const
.
Although, it's possible to call the following code: var window = undefined;
or window = undefined;
, but it won't do anything to the window
object. That's because the setter on the window
object is undefined
and configurable
descriptor is set to false
, disallowing changes and deletion of the window
property. You can see that if you that with the following code:
console.log(Object.getOwnPropertyDescriptor(this, 'window'))
You are still able to declare your own window
variable inside any other lexical context, other than the global one:
const test = () => {
const window = 123;
console.log('in test fn:', window);
}
test()
console.log('in global:', window)
But why can I change the globalThis
then? You can change it because globalThis
is the property of window
. And doesn't seem to have an empty setter and configurable
descriptor is set to true
, allowing changes to this property. All that means, you can do anything you want with globalThis
property :)
console.log(window.hasOwnProperty('globalThis'));
console.log(Object.getOwnPropertyDescriptor(window, 'globalThis'));