It's not a design error, it's more of an interpreter limitation: it's the only way to access the global object that is an instance of the class Window. For example, when you make use of something as everyday as document.getElementById(...), there is not really a variable document, it is an attribute of the global object. That is, you are actually calling
<objetoGlobal>['document'].getElementById
But what happens when you want to access precisely that global object? Well, he doesn't have a known name. So the scenario we have is that there is an object whose attributes we can access, while the entire object is inaccessible because it doesn't have a name. The solution is for one of the attributes to be itself:
In cases where you are working with webWorkers there is no attribute windowon the global object, but you can use self. Of course, it also holds that
self===self.self
Therefore, when you write document, the interpreter understands it as <objetoGlobal>['document']and when you write window.documentit is like writing <objetoGlobal>['window'].document.
Both attributes, windowand selfare read-only, cannot be modified for obvious reasons.
The only scenario where you can't access the window is that you're not in the global context and the local variables selfand have been created window, but you could still access the rest of the global attributes.
This is done to facilitate access to this global variable and prevent it from being confused or lost by plugins or extensions that can create a variable or object called window. For a more extended explanation I recommend that you read its documentation:
The window object has the window object nested and so on ad infinitum. Some libraries check that the window variable is set to know that they are running in a browser and not in node or a webworker.
For this it is not enough to verify the existence of the variable windowthat may have been overwritten. That's why they check nesting as an extra verification step.
This is what the documentation says about the property Window.window:
The point of making the window property refer to the object itself would probably make it easy to refer to the global object. Otherwise you would have to do a var window = this; manual assignment at the top of your script.
Another reason is that without this property you would not be able to write, for example, " window.open(' http://google.com/ ')". You should use "open" (" http://google.com/ ") instead.
What this means is that when we access the object window, we are actually accessing the property windowof an object of type Windowthat is defined globally.
In other words this:
window.open(url);
Or this open(url)(which ultimately translates to window.open(url)). It would be the same as doing this:
var newWindow = new Window();
newWindow.open(url)
But since we already have an object of type Window with the property windowdefined, we can do window.open().
It's not a design error, it's more of an interpreter limitation: it's the only way to access the global object that is an instance of the class
Window
. For example, when you make use of something as everyday asdocument.getElementById(...)
, there is not really a variabledocument
, it is an attribute of the global object. That is, you are actually calling<objetoGlobal>['document'].getElementById
But what happens when you want to access precisely that global object? Well, he doesn't have a known name. So the scenario we have is that there is an object whose attributes we can access, while the entire object is inaccessible because it doesn't have a name. The solution is for one of the attributes to be itself:
<objetoGlobal>['window']=<objetoGlobal>
me<objetoGlobal>['self']=<objetoGlobal>
In cases where you are working with webWorkers there is no attribute
window
on the global object, but you can useself
. Of course, it also holds thatTherefore, when you write
document
, the interpreter understands it as<objetoGlobal>['document']
and when you writewindow.document
it is like writing<objetoGlobal>['window'].document
.Both attributes,
window
andself
are read-only, cannot be modified for obvious reasons.The only scenario where you can't access the window is that you're not in the global context and the local variables
self
and have been createdwindow
, but you could still access the rest of the global attributes.This is done to facilitate access to this global variable and prevent it from being confused or lost by plugins or extensions that can create a variable or object called window. For a more extended explanation I recommend that you read its documentation:
APIWindow
The window object has the window object nested and so on ad infinitum. Some libraries check that the window variable is set to know that they are running in a browser and not in node or a webworker.
For this it is not enough to verify the existence of the variable
window
that may have been overwritten. That's why they check nesting as an extra verification step.This is what the documentation says about the property
Window.window
:What this means is that when we access the object
window
, we are actually accessing the propertywindow
of an object of typeWindow
that is defined globally.In other words this:
Or this
open(url)
(which ultimately translates towindow.open(url)
). It would be the same as doing this:But since we already have an object of type Window with the property
window
defined, we can dowindow.open()
.