On two types of global object/function _javascript techniques in JavaScript

Source: Internet
Author: User
Tags static class hosting

The JavaScript described here refers to the hosting environment in the browser environment. The first is ECMAScript global object, and the second is global objects/functions under the Host Environment (host).

One, core JavaScript built-in object, that is, the ECMAScript implementation provides the object that does not depend on the host environment

These objects have been (instantiated) before the program was executed. ECMAScript is called the Global Object and is divided into the following

1, the global object of the value attribute (value properties of the global objects). There are nan,infinity,undefined.

2, the global object of the Function property (the functions properties of the global objects). Have eval,parseint,parsefloat,isnan,isfinite,decodeuri,encodeduri,encodeuricomponent

3, the global object of the constructor (class) attribute (constructor properties of the the global objects). Have Object,function,array,string,boolean,number,date,regexp,error,evalerror,rangeerror,referenceerror,syntaxerror , Typeerror,urierror.

4, the global object of other attributes (other properties of the global objects) can be seen as a static class in Java, which can be used directly with the class name + dot number + method name. There are math,json.

The ECMAScript specification mentions that these global objects, which have a writable attribute, that is writable true and that the enumeration (enumerable) is false, cannot be enumerated with a for. ECMAScript has such a

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


While the specification mentions the Global object can be rewritten, no one will rewrite them. Just a test here.

Copy Code code as follows:

NaN = 11;
eval = 22;
Object = 33;
Math = 44;

alert (NaN);
Alert (eval);
alert (Object);
Alert (Math);<br>

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,math of the other attribute. The results are as follows

As a result, other than Nan in IE9 (pre3)/safari could not be rewritten, others were rewritten. Here are just a list of four of them that are interested in putting all of the above the Global object one by one under test. The point here is that the core JavaScript built-in objects are generally rewritten, although no one has done so.

The following tests its enumerable

Copy Code code as follows:

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 eject, that is, properties are not enumerated. Interested can be tested with all of the above enumeration of the Global object. Of course, for some browsers such as Firefox, some global object is rewritten and can be enumerated.

Global objects/functions provided by the hosting environment

Most browsers, such as Window,alert,settimeout,document,location, limit their override

Copy Code code as follows:

window = 55;
alert (window);

This sentence in IE will be an error prompted illegal copy, the following pop-up box did not execute. Other browsers still pop up window when window=55 does not exist.

and rewrite alert.

Copy Code code as follows:

alert = 55;
Console.log (alert);

IE under the prompt error, Firefox/chrome/safari/opera unexpectedly was rewritten, from the corresponding console can see output of 55. You can see the global objects/functions provided for the hosting environment, some browsers do not support rewriting, and others can be overridden.

Here are two ways to declare a global variable

Copy Code code as follows:

a1 = 11;
var a2 = 22;

For (A in window) {
If (a== ' A1 ' | | a== ' A2 ') {
Alert (a)
}
}

The above code in IE does not pop-up message box, in IE in the interior is probably as follows
Copy Code code as follows:

Ie
With (Host_object) {//window
With (Global_object) {//global
a1 = 11;
var a2 = 22;
}
}

That is, A1,A2 is the first of the above, the JS engine provides properties on the global object, not the properties on the Window object provided by the second hosting environment. Therefore, A1,A2 does not exist in the for in window of IE. If you provide a reference to an object global object in IE, the following code can pop-up the message box.
Copy Code code as follows:

For (A in Global) {
If (a== ' A1 ' | | a== ' A2 ') {
Alert (a)
}
}

The inside of the Firefox/safari/chrome/opera is probably underneath.
Copy Code code as follows:

Firefox/safari/chrome/opera
With (Host_object) {//window
a1 = 11;
var a2 = 22;
With (Global_object) {//global
}
}

That is, A1,A2 is the second of the above, the host environment provides properties on the Global Object window. As a result, the A1,A2 is present for the in window, and the message box pops up.

Then look at the third party. Declares global variable window.a3 = 33, which is the property that displays the A3 on window as window, so you can get A3 in all browsers for the in window.

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.