For any JavaScript program, when the program starts running, the JavaScript interpreter will initialize a global object for use by the program. The functions of the global object provided by JavaScript itself include:
1. The global object has some commonly used attribute values. Such as undefined, Infinity and NaN.
2. The global object has some commonly used attribute objects. For example, Math, JSON and Number objects are all properties of the global object.
3. The global object provides some global functions for calling. For example, isNaN(), isFinite(), parseInt(), eval(), etc.
4. The global object provides some global constructors, that is, global classes. For example, Date(), RegExp(), String(), Object(), Array(), etc.
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; therefore, when programming browser-side JavaScript, you only need to care about the window global object. That’s it.
For this in a JavaScript program, if this does not belong to any function, then this refers to the JS global object; if it is a JS program running on the browser, then this refers to the window global object.
If this this belongs to a function, then this refers to the object that calls the function. If function is just an ordinary function in this case, rather than a method of a certain class, then there are two possibilities for the reference of this:
1. In the ECMAScript 3 standard and the non-strict mode of the ECMAScript 5 standard, this refers to the global object.
2. In the strict mode of the ECMAScript 5 standard, this refers to undefined.
According to this feature, you can use the following code to determine whether you are currently in strict mode:
If a global variable is created in a JavaScript program, the global variable will become a property in the global object.
Experiment
x = "test";
console.log(a.x);//access newly created global variable value