Two types of global objects/functions in JavaScript

Source: Internet
Author: User
Tags alphanumeric characters hosting

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


One, core JavaScript built-in object, the ECMAScript implementation provides an object that is not dependent on the hosting environment

These objects already exist (instantiated) before the program executes. ECMAScript is known as the global object, which is divided into the following

1, the Value property of the globally objects (value properties of the). There are nan,infinity,undefined.
2, the global object of the Function property (the functions properties of the the global objects). Global object with Eval,parseint,parsefloat,isnan,isfinite,decodeuri,encodeduri,encodeuricomponent
3, constructor (Class) Property ( Constructor Properties of the Global Object). 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 World object), can be seen as a static class in Java, can be used directly with the class name + dot + method name. There are math,json. The

ECMAScript specification mentions that these global objects are writable properties, that is, writable is true, and the enumeration (Enumerable) is false, that is, you cannot use the for in enumeration. ECMAScript has a

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


Although the specification mentions that the Global object can be rewritten, there is no one to rewrite them. This is just a test.

123456789 NaN    = 11;eval   = 22;Object = 33;Math   = 44;alert(NaN);alert(eval);alert(Object);alert(Math);<br>

The global object that takes a value attribute, the global object of the Function property, the constructor (class) property, and the global object Nan,eval,object,math the other properties. The results are as follows

The results can be seen except that nan in IE9 (pre3)/safari cannot be overridden, and others are rewritten. Here are just a list of four, interested can be the above all the Global object one by one under test. The point here is that core JavaScript built-in objects can be rewritten, although no one is doing so.

The following test is the enumerable of

123456789101112 for(varinNaN){    alert(a);}for(varineval){    alert(a);}for(varinObject){    alert(a);}for(varinMath){    alert(a);}

All browsers are not ejected, that is, the property is not enumerated. It is interesting to test all of the above enumerations of the Global object. Of course, for some browsers such as Firefox, some global objects can be enumerated after being rewritten.


Second, the host environment provides global objects/functions

Like Window,alert,settimeout,document,location, most browsers will restrict their rewriting

12 window = 55;alert(window);

This sentence in IE will error prompt illegal copy, the following pop-up box is not executed. Other browsers, when Window=55 does not exist, still pop up the window.

and rewrite the alert.

12 alert = 55;console.log(alert);

IE prompt error, Firefox/chrome/safari/opera unexpectedly be rewritten, from the corresponding console can see the output of 55. You can see that there are global objects/functions available to the hosting environment, some browsers do not support overrides, and others can be overridden.

Here are two ways to declare a global variable

12345678 a1 = one; var  a2 = $;  for (a  in  window) { &NBSP;&NBSP;&NBSP;&NBSP; if (a== ' A1 ' | | a== ' A2 ' ) {

The above code does not pop up the message box in IE, the inside IE is probably as follows

1234567 //ie with (host_object) { //window &NBSP;&NBSP;&NBSP;&NBSP; with (global_object) { //global &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; a1 = one; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; var  a2 = $; &NBSP;&NBSP;&NBSP;&NBSP; }&NBSP;&NBSP; }

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 for the in window of IE. If a reference to the object global object is provided in IE, the following code may pop up the message box.

12345 for(a inGlobal){    if(a==‘a1‘||a==‘a2‘){        alert(a)    }}

Inside the Firefox/safari/chrome/opera, it's probably underneath.

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

That is, A1,A2 is the second, as mentioned above, the property on the window of the global object provided by the hosting environment. Therefore, the A1,A2 is present at window, and the message box pops up.
Look at the third-party way of declaring the global variable window.a3 = 33, this is the display of the A3 hanging on the window as a window property, so in all browsers in the for window can get to A3.

The global object is a predefined object, as a placeholder for JavaScript global functions and global properties. By using global objects, you can access all of the other predefined objects, functions, and properties. The global object is not a property of any object, so it does not have a name.

A global object is just an object, not a class. There is neither a constructor function nor a new global object to instantiate.

Top-level functions (global functions)

Ff:firefox, N:netscape, Ie:internet Explorer

function Description FF N IE
decodeURI () Decodes the URI of an encoding. 1 4 5.5
decodeURIComponent () Decodes a coded URI component. 1 4 5.5
encodeURI () Encodes a string as a URI. 1 4 5.5
encodeURIComponent () Encodes a string as a URI component. 1 4 5.5
Escape () Encodes a string. 1 - 3
Eval () Computes the JavaScript string and executes it as script code. 1 2 3
GetClass () Returns the Javaclass of a javaobject.
Isfinite () Checks if a value is a number that has a poor size. 1 4 4
IsNaN () Checks whether a value is a number. 1 2 3
Parsefloat () Parses a string and returns a floating-point number. 1 2 3
parseint () Parses a string and returns an integer. 1 2 3
Unescape () Decodes a string encoded by escape (). 1 - 3
Top-level properties (global properties)

Ff:firefox, N:netscape, Ie:internet Explorer

Properties Description FF N IE
Infinity Represents the numeric value of the positive infinity. 1 4 4
Java Represents a javapackage at the Java.* package level.
NaN Indicates whether a value is a numeric value. 1 4 4
Packages The root Javapackage object.
Undefined Indicates a value that is not defined. 1 4 5.5
Note:

The encodeURI () and encodeURIComponent () methods are used to encode the URI (Uniform Resource Identifier) passed to the browser. A valid URI cannot contain certain characters, such as spaces. These two methods are used to encode URIs so that all non-valid characters are replaced with specialized UTF-8 encodings, allowing the browser to still accept and understand them.

The encodeURI () method is used to process the complete URI (for example,Http://www.wrox.com/illegal value.htm), and encodeURIComponent () is used to handle A fragment of the URI (such as illegal value.htm in the preceding URI ). The main difference between the two methods is that the encodeURI () method does not encode special characters in the URI, such as colons, front slashes, question marks, and pound signs, while encodeuricomponent () encodes all non-standard characters it discovers . For example:
var SUri = "Www.sohu.com/abc def i. aspx";
Alert (encodeURI (SUri));
Alert (Encodeuricomponet (SUri));

This code outputs two values:
Www.sohu.com/abc%20def%20%E6%88%91.aspx
Www.sohu.com%2Fabc%20def%20%E6%88%91.aspx

As you can see, in addition to NULL, the first URI has no change, and the space is replaced by % 20. all non-alphanumeric characters in the second URI are replaced with their corresponding encoding, which basically makes the URI useless. This is why encodeURI () can handle the full URI, and encodeURIComponent () can only handle strings appended to the end of an existing URI.

Naturally, there are also two methods for decoding encoded URIs, namely decodeURI () and decodeuricomponent (). As you can see, these two methods do exactly the opposite of their corresponding methods. The decodeURI () method decodes only the characters replaced with the encodeURI () method. For example,%20 will be replaced with a space, and%23 will not be replaced because it represents the pound sign (#), andencodeURI () does not replace this symbol. Similarly,decodeuricomponent () decodes all encodeuricomponent () encoded characters, meaning that it decodes all the special values. For example:

This code outputs two values:

In this example, the variable URI holds a string encoded with encodeURIComponent (). The generated value describes what happens when you apply the two decoding methods. The first value is output by decodeURI () and the%20 is replaced by a space. The second value is output by decodeuricomponent (), replacing all of the special.

These URI methods encodeURI (),encodeuricomponent (),decodeURI () , and decodeuricom-ponent () replaces the Escape () and unescape () methods of the BOM. URI methods are preferable because they encode all Unicode symbols, and the BOM method can encode only the ASCII symbols correctly. Avoid using the escape () and unescape () methods as much as possible.

Sample:var s= "webform1.aspx?a=" + encodeuricomponent (' abc &=*def me ') + "&b=" + encodeURIComponent (' abc def & = * Large set ');
alert (s);
Window.location.href=s;

Two types of global objects/functions in JavaScript

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.