global object¶Javascript provides a unique global object accessible to all execution contexts. The global object has a number of properties predefined. Some of this properties are required by the ECMA standard and some are specific to the platform. See here the relevant documentation for Node.js and here for the HTML DOM.
Javascript provides two ways to access the properties in the global object:
Javascript platforms may include a global property whose value is the global object itself. In the HTML DOM, this property is window; in Node.js, the property is global property. This global property can be used to access properties in the global object from different execution contexts, for example:
(function() {
global.myGlobalProperty = "Hello, World!";
})()
global.myGlobalProperty;
NaN and isNaN are properties defined in the global object. NaN is set to the "Not-a-Number" value defined by the IEEE 754 standard for floating-point numbers. And isNaN is set to a function that returns whether a number is NaN. Here is an example that illustrates how these properties are implicitly accessible from different execution contexts:
(function f() {
return isNaN(NaN);
})()
Note that although accessing global properties in this way is very convenient, it is also riddle with unexpected side effects:
(function() {
var isNaN = function(number) {
return !global.isNaN(number);
};
return isNaN(NaN);
})()
(function() {
isNaN = function(number) {
return !global.isNaN(number);
};
return isNaN(NaN);
})()