∟The "Global" Object Type - The Invisible Global Container
This section provides a quick description and a tutorial example script of the 'Global' built-in object type, which is used to create a default invisible global object to hold all global properties and functions.
The "Global" object type is probably the most mysterious built-in object type.
It has the following main features:
"Global" is a reserved object type. You can not create your own "Global" objects.
The "Global()" constructor is not accessible.
The host environment will create a single default "Global" object, which is not assigned to any reference variables.
So this "Global" object is invisible.
However, this invisible "Global" object is visible in the global execution context (outside any functions) by using
the "this" keyword.
You can use the expression of "this instanceof Global" to check the object type of "this",
because the "Global()" is not accessible.
The "Global" object is created to hold all built-in properties and functions provided by the host environment, like
"NaN", "eval()", etc..
Technically built-in properties or functions are all defined as properties of the "Global" object.
But you can use them directly without the reference to the "Global" object.
Here is a tutorial sample script showing you some of those features:
// Global_Object_Type.html
// Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
// Global() constructor is not accessible
// var myGlobal = new Global();
println("\nAbout this \"this\":");
println(" Type = "+(typeof this));
println(" Instance Of Object: "+(this instanceof Object));
// "instanceof Global" is not allowed
// println(" Instance Of Global: "+(this instanceof Global));
// toString() will us more
println(" toString() = "+this.toString());
// Accessing a global property directly
println("\nChecking NaN:");
println(" NaN: Type = "+(typeof NaN)
+ ", Value = "+NaN);
// Accessing a global property indirectly
println("\nChecking this.NaN:");
println(" this.NaN: Type = "+(typeof this.NaN)
+ ", Value = "+this.NaN);
// Testing a global function
println("\nTesting eval() in two ways:");
println(" eval('7*7') = "+eval('7*7'));
println(" this.eval('7*7') = "+this.eval('7*7'));
If you run this script with "jrunscript", you will get:
About this "this":
Type = object
Instance Of Object: true
toString() = [object Global]
Checking NaN:
NaN: Type = number, Value = NaN
Checking this.NaN:
this.NaN: Type = number, Value = NaN
Testing eval() in two ways:
eval('7*7') = 49
this.eval('7*7') = 49
"this.toString()" proves that "this" is a "Global" object.