Reading notes global objects in the-javascript

Source: Internet
Author: User

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

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.