Javascript Global Objects _ Basics

Source: Internet
Author: User
Tags decode all eval numeric value alphanumeric characters

Global Object

The global object is the most specific object in the ECMAScript because it does not actually exist at all. If you try to write the following code, you get an error:

The error message shows that global is not an object, but was it not that global was an object? That's right. The main concept to understand here is that there is no independent function in ECMAScript, and all functions must be methods of an object. The functions described earlier in this book, such as isNaN (), Isfinite (), parseint (), and parsefloat (), all look like independent functions. In fact, they are all methods of the global object. And the global object approach is more than that.

The encodeURI () and encodeURIComponent () methods are used to encode the URI (Uniform Resource Identifier) that is passed to the browser. A valid URI cannot contain certain characters, such as spaces. These two methods are used to encode the URI so that the browser can still accept and understand them by replacing all of the UTF-8 characters with a special-purpose encoding.

The encodeURI () method is used to process the complete URI (for example, Http://www.wrox.com/illegal value.htm) and encodeURIComponent () A fragment used to process a URI (such as the illegal value.htm in the previous URI). The main difference between the two methods is that the encodeURI () method does not encode special characters in the URI, such as colons, forward slashes, question marks, and sterling notation, while encodeURIComponent () encodes all non-standard characters it finds. For example:

This code outputs two values:

As you can see, with the exception of the null, the first URI has no change, and the space is replaced with%20. All non-alphanumeric characters in the second URI are replaced with their corresponding encodings, making the URI almost useless. This is encodeURI () that can handle the full URI, while encodeURIComponent () only handles the reason for strings attached to the end of existing URIs.

Naturally, there are also two methods for decoding encoded URIs, namely decodeURI () and decodeURIComponent (). As you would expect, these two methods do exactly the opposite of their corresponding methods. The decodeURI () method decodes only characters that are 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 (#) and encodeURI () does not replace the symbol. Similarly, decodeuricomponent () decodes all encodeuricomponent () encoded characters, meaning that it will decode all the special values. For example:

This code outputs two values:

In this example, the variable URI holds a string encoded with encodeURIComponent (). The resulting value illustrates what happens when two decoding methods are applied. The first value is output by decodeURI (), and the%20 is replaced with a space. The second value is output by decodeURIComponent (), replacing all the special.

These URI methods encodeURI (),encodeuricomponent (),decodeURI () , and decodeuricom-ponent () Replaces the escape () and unescape () method of the BOM. URI methods are preferable because they encode all Unicode symbols, and the BOM method encodes only the ASCII symbols correctly. Try to avoid using the escape () and unescape () methods.

The last method may be the most powerful method in the entire ECMAScript language, the eval () method. The method, like the entire ECMAScript interpreter, takes a parameter, the ECMAScript (or JavaScript) string to be executed. For example:

The functionality of this line of code is equivalent to the following code:

When the interpreter discovers the eval () call, it interprets the argument as a true ECMAScript statement, and then inserts it into the location of the function. This means that the eval () call internally referenced variable can be defined outside of the parameter:

Here, the variable msg is defined outside the context of the eval () call, and the warning still shows the text "Hello World" because the second line of code is replaced with a line of real code. Similarly, you can call an internal definition function or variable in eval (), and then reference in code outside the function:

Here, the function sayhi () is defined within the eval () call. Because the call will be replaced with a real function, you can still invoke Sayhi () in the next row.

This function is very powerful, but it is also very dangerous. Use eval () with extreme care, especially if you pass the data entered by the user. A malicious user may insert a value (called Code injection) that is harmful to the security of the site or application.

The global object is not only a method, it also has attributes. Remember those special values undefined, Nan and infinity? They are all properties of the global object. In addition, the constructors of all local objects are also properties of the global object. The following table describes all the properties of the global object in more detail:

Property

Description

Undefined

Literal amount of undefined type

NaN

Private value for non-number

Infinity

Private numeric value for infinity

Object

Constructor of Object

Array

The constructor of the array

Function

Constructor for function

Boolean

Boolean constructor

String

The constructor for string

Number

The constructor for number

Date

The constructor for date

Regexp

Constructor for RegExp

Error

The constructor of the error

Evalerror

Constructor for Evalerror

Rangeerror

Constructor for Rangeerror

Referenceerror

Constructor for Referenceerror

SyntaxError

Constructor for SyntaxError

TypeError

Constructor for TypeError

Urierror

Urierror's Construction letter

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.