Skip to content Skip to sidebar Skip to footer

Why Can We Access Window.document Property Without Specifying Window Object?

As a JavaScript newcomer I see a lot of magic which isn't explained in books. For example, why can I write document.getElementById('one'); When document is a property of window? F

Solution 1:

window is the Global object in a browser and because of the way scope works in JavaScript, the Global object will always be the last place that is searched for something. So, omitting window is OK because it will wind up being found at the end of the "scope chain".

document is a property of window and, as such, you do not need to qualify it with window for it to be found because when the browser reaches window and still hasn't found what it's looking for, it will look at the properties of window and find document there.


Solution 2:

window represents the browser's window. All global JavaScript objects, functions, and variables automatically become members of the window object.

document is also a property of global object and hence can be accessed as window.document or document.

For more information, you can refer here.


Post a Comment for "Why Can We Access Window.document Property Without Specifying Window Object?"