The Global object

From Web Education Community Group
Jump to: navigation, search

First, it should be noted that a basic understanding of Objects are assumed so, if you are unsure of what is meant by the term Object, you can read more about Objects in the general programming section.

What is the global object?

(Note: All discussions of the global object below assumes that the code will be executed in a user agent (browser) environment)

In short, the global object is the Window object, and is implicit.

If we dig a little deeper, we can say that the global object is the parent object that houses all the other Objects that contain the functions and properties common to the JavaScript language and, which allow developers to interact and manipulate the current document or DOM (Document Object Model).

The Window object is what is referred to when people talk about the Global object but, in reality there are between 40 - 45 global objects all of which we will deal with in other parts of the curriculum.

What do you mean it’s implicit?

So what does it mean when it is said that the Window object is implicit? Consider the following line of JavaScript:

document.getElementById(“implicit”);

Under the hood this is interpreted as:

window.document.getElementById(“implicit”);

This is also why the following two lines of code will achieve the exact same end result:

window.alert(“Hello World”);
alert(“Hello World”);

Because Window is the top level Object in the hierarchy, when calling functions or accessing properties without specifying any other parent, Window will be assumed. This then goes a great deal towards demonstrating why it is important to never use the JavaScript [reserved words] for your own functions and properties, to always [define all your variables and never, pollute the global scope].