Two types of global objects/functions in Javascript

Source: Internet
Author: User

The javascript mentioned here refers to the browser environment, including the host environment. The first is ecmascript global object, and the second is the Global Object/function under the host environment (host.

1. Core JavaScript built-in objects, that is, objects provided by ecmascript that do not depend on the host environment

These objects already exist (instantiated) before the program is executed. Ecmascript is called the Global Object and is divided into the following types:

1. Value properties of the global object ). There are Nan, infinity, and undefined.
2. Function properties of the global object ). Eval, parseint, parsefloat, isnan, isfinite, decodeuri, encodeduri, encodeuricomponent
3. constructor properties of the Global Object ). Object, function, array, String, Boolean, number, date, Regexp, error, evalerror, rangeerror, referenceerror, syntaxerror, typeerror, urierror.
4. The Global Object of other properties (other properties of the Global Object) can be seen as a static class in Java, which can be directly used by class name + vertex number + method name. There are math and JSON.

The ecmascript specification mentions that these global objects have the writable attribute, that is, writable is true, enumerable is false, that is, for in enumeration cannot be used. Ecmascript has such a paragraph

Unless otherwise specified, the standard built-in properties of the global object have attributes {[[writable]: True, [[enumerable]: false, [[retriable]: true }.

Although the rules mentioned that the global object can be rewritten, no one will rewrite them. Here we will just do a test.

NaN    = 11;eval   = 22;Object = 33;Math   = 44;alert(NaN);alert(eval);alert(Object);alert(Math);

The Global Object of the value attribute, the Global Object of the function attribute, the Global Object of the constructor (class) attribute, and the Global Object Nan, Eval, object, and math of other attributes respectively. The result is as follows:

The result shows that Nan cannot be overwritten in ie9 (pre3)/safari, and all others are overwritten. Here we only list four items. If you are interested, you can test all the above global objects one by one. What I want to express here is that the core JavaScript built-in objects can be rewritten, although no one does this.

The following is a test of the scalability.

for(var a in NaN){alert(a);}for(var a in eval){alert(a);}for(var a in Object){alert(a);}for(var a in Math){alert(a);}

All Browsers Do not pop up, that is, the properties are not enumerated. If you are interested, you can test the enumeration of all the above global objects. Of course, some browsers, such as Firefox, can be enumerated after some global objects are overwritten.

2. Global Objects/functions provided by the host environment

For example, window, alert, setTimeout, document, location, etc.

window = 55;alert(window);

The error message "invalid copy" appears in IE, and the prompt box that appears later is not executed. If window = 55 does not exist in other browsers, the window is displayed.

Then rewrite alert.

alert = 55;console.log(alert);

An error is reported in IE. Firefox, chrome, Safari, and opera have been rewritten. The output is 55 on the corresponding console. We can see that some Browsers Do not support Rewriting for global objects/functions provided by the host environment, while others can be rewritten.

The following two methods are used to declare global variables:

a1 = 11;var a2 = 22;for(a in window){if(a=='a1'||a=='a2'){alert(a)}}

The above Code does not pop up a message box in IE, which is roughly as follows in IE:

//IEwith(host_object){//windowwith(global_object){//Globala1 = 11;var a2 = 22;}}

A1 and A2 are the first type mentioned above. Attributes on the global object provided by the JS engine, rather than those on the window object provided by the second host environment. Therefore, A1 and A2 do not exist in IE for in window. If IE provides a reference to the Global Object of the object, the following code may pop up the information box.

for(a in Global){if(a=='a1'||a=='a2'){alert(a)}}

In Firefox, Safari, chrome, and opera, the following figure appears:

//Firefox/Safari/Chrome/Operawith(host_object){//windowa1 = 11;var a2 = 22;with(global_object){//Global}}

A1 and A2 are the second type mentioned above. The properties of the Global Object window provided by the host environment. For in window, both A1 and A2 exist. The information box is displayed.
The third-party method declares the global variable window. A3 = 33. This is the display that hangs A3 on the window as the property of the window. Therefore, A3 can be obtained in all browsers for in window.

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.