Javascript Global Object

Source: Internet
Author: User
Tags alphanumeric characters

GlobalObject

Global objects are the most special objects in ecmascript, because they do not exist at all. If you try to write the followingCode, An error is returned:

The error message shows that global is not an object, but didn't Global be an object just now? Yes. The main concept to be understood here is that there are no independent functions in ecmascript, and all functions must be methods of an object. Functions described earlier in this book, such as isnan (), isfinite (), parseint (), and parsefloat (), all look like independent functions. In fact, they are all global object methods. Besides, there are more methods for global objects.

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 the browser can still accept and understand them by replacing all invalid characters with a dedicated UTF-8 encoding.

The encodeuri () method is used to process the complete uri (for example, the http://www.wrox.com/illegal value.htm), while encodeuricomponent () is used to process a piece of the 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, front slashes, question marks, and pound symbols, while encodeuricomponent () encode all non-standard characters it finds. For example:

This code outputs two values:

We can see that except for spaces, the first URI does not change, and the space is replaced with % 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 process complete Uris, while encodeuricomponent () can only process strings appended to the end of an existing Uri.

Naturally, there are two methods used to decode the encoded Uri, namely decodeuri () and decodeuricomponent (). As expected, the two methods do the opposite. The decodeuri () method only decodes the characters replaced by the encodeuri () method. For example, % 20 will be replaced with a space, while % 23 will not be replaced because it represents the pound sign (#), and encodeuri () will not be replaced. Similarly, decodeuricomponent () decodes all characters encoded by encodeuricomponent (), meaning that it decodes all special values. For example:

This code outputs two values:

In this example, the variable URI is a string encoded with encodeuricomponent. The generated value indicates what happens when two decoding methods are applied. The first value is output by decodeuri () and % 20 is replaced with a space. The second value is output by decodeuricomponent (), replacing all the special values.

These URI MethodsEncodeuri (),Encodeuricomponent (),Decodeuri ()AndDecodeuricom-ponent ()Replaced by BOMEscape ()AndUnescape ()Method. The URI method is more desirable because it will encode all Unicode characters, while the BOM method can only correctly encode ASCII characters. Avoid usingEscape ()AndUnescape ()Method.

The last method may be the most powerful method in the entire ecmascript language, namely the eval () method. This method is like an explanation of ecmascript.ProgramTo accept a parameter, that is, the ecmascript (or Javascript) string to be executed. For example:

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

When the interpreter finds an eval () call, it will interpret the parameter as a true ecmascript statement and insert it into the position of the function. This means that variables referenced inside the eval () call can be defined apart from parameters:

Here, the variable MSG is defined outside the eval () call environment, and the warning still displays the text "Hello world ", because the second line of code will be replaced with a real line of code. Similarly, you can call an internal defined function or variable in eval () and reference it in code outside the function:

Here, the sayhi () function is defined internally in the eval () call. Because this call will be replaced with a real function, you can still call sayhi () in the next line ().

This function is very powerful, but it is also very dangerous. UseEval ()Be extremely careful, especially when passing user input data to it. Malicious users may insert values that are harmful to the security of websites or applications (called code injection ).

The global object does not only have methods, but also has attributes. Do you still remember the special undefined, Nan, and infinity values? They are all attributes of global objects. In addition, constructors of all local objects are also attributes of global objects. The following table details all attributes of the global object:

Genus

Description

Undefined

Undefined literal

Nan

Non-numeric value

Infinity

Special Value of an infinite value

Object

Object constructor

Array

Array Constructor

Function

Function Constructor

Boolean

Boolean Constructor

String

String Constructor

Number

Number Constructor

Date

Date Constructor

Regexp

Regexp Constructor

Error

Error Constructor

Evalerror

Evalerror Constructor

Rangeerror

Rangeerror Constructor

Referenceerror

Referenceerror Constructor

Syntaxerror

Constructor of syntaxerror

Typeerror

Typeerror Constructor

Urierror

Urierror Constructor

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.