Variables as Properties
You may have noticed by now that there are a
lot of similarities in JavaScript between variables and the
properties of objects. They are both assigned the same way, they are
used the same way in JavaScript expressions, and so on. Is there
really any fundamental difference between the variable
i and the property i of an
object o? The answer is no. Variables in
JavaScript are fundamentally the same as object properties.
The Global Object
When the JavaScript interpreter starts up, one of the first things it does, before executing any JavaScript code, is create a global object. The properties of this object are the global variables of JavaScript programs. When you declare a global JavaScript variable, what you are actually doing is defining a property of the global object.
The JavaScript interpreter
initializes the global object with a number of properties that refer
to predefined values and functions. For example, the
Infinity, parseInt, and
Math properties refer to the number infinity, the
predefined parseInt( ) function, and the
predefined Math object. You can read about these global values in the
core reference section of this book.
In top-level code (i.e., JavaScript code that is not part of a
function), you can use the JavaScript keyword this
to refer to the global object. Within functions,
this has a different use, which is described in
Chapter 7.
In client-side JavaScript, the Window object serves as the global object for all JavaScript code ...