Whenever I see a website on the browser an instance of javascript is running. And I can declare a global variable in the console (DevTools);
var a = 1234567890;
This variable has been declared in global scope such that I can get the value of the variable like so;
> a
1234567890
However, I can also do this;
> window.a
1234567890
Am I understanding it correctly that the window
object is the object that contains all the global variables within the website instance on the browser? If so to what scope does the window object belong? This is confusing me a little bit;
> window
Window {top: Window, window: Window, location: Location, external:, ...}
> window.window
Window {top: Window, window: Window, location: Location, external:, ...}
> window.window.window
Window {top: Window, window: Window, location: Location, external:, ...}
Is the window
object the ultimate global object and does that have an object called window
that refers back to itself?
All global variables become properties of the window object.
And all of the core JavaScript functions are methods of the window object.
Yes, and yes. This, for instance, returns
true
:You can, if you are interested, get a list of all the properties of the
window
object (and hence all global variables) withObject.keys
:Note, however, that if you are spending too much time thinking about global variables, there is probably a problem with the architecture of your code.
Yes, the
window
object isThe Global Object(§15.1 ES5 Specification)
Yes,
Window
is the root element of the DOM (Document Object Model) object hierarchy.