Global objects in JavaScript Introduction to _javascript Tips

Source: Internet
Author: User

For any JavaScript program, the JavaScript interpreter Initializes a global object for use by the program when the program starts running. The functionality of the global objects provided by this JavaScript itself includes:

1. Global objects have some commonly used 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 to the outside, so when you do 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, and if it is the JS program that is running on the browser side, then this refers to the window global object.

If this this belongs to a function, then this refers to the object that invokes the function. In this case, the function is just a normal one, not a method of a class, and there are two possible indications of this:

1. In the ECMAScript 3 Standard and the ECMAScript 5 standard, this refers to the global object.
2. In the strict mode of the ECMAScript 5 standard, this refers to the undefined.

Based on this feature, you can use the following code to determine whether you are currently in strict mode:


Copy Code code as follows:

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


If you create a global variable in a JavaScript program, the global variable becomes a property in the global object.

Experiment


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.