HTML and CSS Reference
In-Depth Information
In addition to the window property (in browsers), the global object can be
accessed as this in the global scope. Listing 5.14 shows how window relates to
the global object in browsers.
Listing 5.14 The global object and window
var global = this;
TestCase("GlobalObjectTest", {
"test window should be global object": function () {
assertSame(global, window);
assertSame(global.window, window);
assertSame(window.window, window);
In the global scope, the global object is used as the variable object, meaning
that declaring variables using the var keyword results in corresponding properties
on the global object. In other words, the two assignments in Listing 5.15 are almost
Listing 5.15 Assigning properties on the global object
var assert = function () { /* ... */ };
this.assert = function () { /* ... */ };
These two statements are not fully equivalent, because the variable declaration
is hoisted, whereas the property assignment is not.
5.3.5 The Scope Chain
Whenever a function is called, control enters a new execution context. This is even
true for recursive calls to a function. As we've seen, the activation object is used for
identifier resolution inside the function. In fact, identifier resolution occurs through
the scope chain, which starts with the activation object of the current execution
context. At the end of the scope chain is the global object.
Consider the simple function in Listing 5.16. Calling it with a number results
in a function that, when called, adds that number to its argument.
Listing 5.16 A function that returns another function
function adder(base) {
return function (num) {
Search WWH ::

Custom Search