Home >Web Front-end >JS Tutorial >What is a JavaScript global object? What properties does the global object have?

What is a JavaScript global object? What properties does the global object have?

伊谢尔伦
伊谢尔伦Original
2017-07-27 13:44:053800browse

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 (constructor), 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 performing browser-side JavaScript programming, Just care about the window global object.

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 in the non-strict ECMAScript 5 standard In mode, 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:

var strict = (function(){return !this;}());

If a global variable is created in a JavaScript program, then this global variable will become a global object in a property.

Experiment

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


The above is the detailed content of What is a JavaScript global object? What properties does the global object have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn