Home  >  Article  >  Web Front-end  >  Introduction to global objects in JavaScript_javascript tips

Introduction to global objects in JavaScript_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:22:321211browse

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:


Copy code The code is as follows:

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


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

Experiment


Copy code 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

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