Why can't a window or document be set to undefined or null?

This may be a stupid question, but I did not find an answer to it. Why can't we do the following?

window = undefined 

OR

 document = undefined 

I know that these are globals and are available in browsers, but when thinking about how JavaScript works, is this not possible? Are they reviewed every time we try to access them?

I am also interested to know how window or document objects remain as they are even after setting them to a random value ... maybe number or undefined or null .

+6
source share
2 answers

According to standard :

The window attribute should return a Window object of the Window object viewer. The document attribute should return the Window object of the latest Document object.

The Window value is the context in which all your scripts are evaluated. If it was writable, then the above will not be executed, and the implementation will not follow the specification, so it is not writable.
For similar reasons, you can add properties to the Document , but you cannot override it.

You can verify this by looking at the IDL:

 [Unforgeable] readonly attribute WindowProxy window; [Unforgeable] readonly attribute Document document; 
+4
source

window is a context. You cannot do this = something else. document is a window property. It is not writable or customizable.

 Object.getOwnPropertyDescriptor( window, 'document' ); 

Output

 Object {value: document, writable: false, enumerable: true, configurable: false} 
0
source

Source: https://habr.com/ru/post/987072/


All Articles