For any JavaScript program, when the program starts running, the JavaScript interpreter Initializes a global object for the program to use. The functionality of the global objects provided by this JavaScript itself includes:
1. Global objects have some common property values. such as Undefined, infinity, and Nan.
2. Global objects have some common property objects. For example, the math, JSON, and number objects are properties of the global object.
3. Global objects provide some global functions for invocation. such as isNaN (), Isfinite (), parseint (), and Eval ().
4. Global objects provide some global constructors (constructor), or global classes. such as date (), RegExp (), String (), Object (), and Array ().
In addition to the JS global object, there is another global object for JavaScript programs running on the browser side: window. The window Global object provides many properties and methods related to the current window and page. In addition to these browser-related global properties and methods, the Window object also encapsulates the JS global object and exposes the properties and interfaces of the JS global object, so when programming browser-side JavaScript, you only need to care about the window global object.
For this in JavaScript programs, if this does not belong to any function, then this refers to the JS global object, and if it is a JavaScript program running on the browser, this is the window global object.
If this is part of a function, then this refers to the object that called the function. If the function in this case is just a normal, rather than a method of a class, there are two possible ways to refer to this:
1. In the ECMAScript 3 standard, and the ECMAScript 5 standard non-strict mode, this refers to the global object.
2. In the strict mode of the ECMAScript 5 standard, this refers to undefined.
Depending on this feature, you can use the following code to determine whether you are currently in strict mode:
The code is as follows:
var strict = (function () {return!this;} ());
If you create a global variable in a JavaScript program, this global variable becomes a property in the global object.
Experiment
The code is as follows:
var a = this;
Console.log (a);//window object
Console.log (a.outerwidth);//access Window object ' s attribute
Console.log (A.isnan);//access JS Global object ' s attribute
x = "Test";
Console.log (a.x);//access newly created global variable value
Reading notes global objects in the-javascript