Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This might be a silly question but I haven't found an answer to it. Why can't we do the following?

window = undefined

OR

document = undefined

I know those are globals and are available in browsers but thinking of how JavaScript works, is it not possible? Are those re-evaluated every time we try to access them?

I am also interested in knowing how the window or document objects remain what they are even after setting them to a random value... may be a number or undefined or null.

like image 579
Praveen Puglia Avatar asked May 12 '15 16:05

Praveen Puglia


1 Answers

According to the standard:

The window attribute must return the Window object's browsing context's WindowProxy object. The document attribute must return the Window object's newest Document object.

Meaning window is the context in which all of your scripts are evaluated. If it was writable then the above wouldn't hold and the implementation wouldn't follow the spec, therefore it isn't writable.
For similar reasons you can add properties to document but you can't override it.

You can verify this by looking at the IDL:

[Unforgeable] readonly attribute WindowProxy window;
[Unforgeable] readonly attribute Document document;
like image 116
Etheryte Avatar answered Oct 06 '22 01:10

Etheryte