JavaScript stuff. (ii) Global object usage

Source: Internet
Author: User

1. Global object: All properties and methods defined within the global scope are properties of the global object
2. The global object is not used directly and cannot be created with the new operator, which is created when the Scripting engine is initialized and immediately makes its methods and properties available.
For example: Isnan,isfinite (), parseint (), parsefloat () are all methods of the global object
Here are some of the more important ways

(1) URI encoding decoding: encodeURI () \encodeuricomponent (), Decodeuri\decodeuricomponent ()
encodeURI () and encodeURIComponent () are used to encode URIs (Universal Resource Identifiers) for sending to the browser.
Browsers cannot understand and receive special characters, such as spaces, so the two methods use UTF-8 encoding to replace invalid characters in the URI to enable the browser to recognize.
encodeURI (): used primarily for the entire URI and does not encode special characters that belong to the URI itself, such as colons, forward slashes, question marks, well numbers
encodeURIComponent (): used primarily to encode a paragraph in a URI, encoding any non-standard characters it discovers.

var uri = "Http://www.baidu.com/aa value#query";//Only spaces are replaced with Console.log (encodeURI (URI));  Output: Http://www.baidu.com/aa%20value#query, the space is encoded into%20//using the corresponding encoding to replace all non-alphabetic characters console.log (encodeURIComponent (URI)); Output: http%3a%2f%2fwww.baidu.com%2faa%20value%23query//Best Practices://encodeuri:  use//encodeuricomponent () for the entire URI:  use////decodeuri () for the string following the URI: decodes a character encoded with encodeURI//decodeuricomponent (): Decodes a character encoded with encodeuricomponent var Uri2 = "Http://www.baidu.com/aa%20value#query"; Console.log (decodeURI (URI2));  HTTP://WWW.BAIDU.COM/AA Value#queryvar uri3 = "Http%3a%2f%2fwww.baidu.com%2faa%20value%23query"; Console.log ( decodeURI (Uri3));  Http%3a%2f%2fwww.baidu.com%2faa Value%23queryconsole.log (decodeURIComponent (Uri3));  HTTP://WWW.BAIDU.COM/AA Value#query

(2) eval (): receives a parameter, the JavaScript string, similar to a ECMAScript parser:

Eval ("Alert (' Hello,nodejs ')");  Equivalent to alert (' Hello,nodejs ');//The Code executed in eval () is considered part of the execution environment that contains the call, and the executed code has the same scope chain as the Eval statement execution environment var str = "Hello,nodejs"; Eval ("alert (str)"); Alert ("Hello,nodejs"); eval ("function hello () {alert (' Hello,world ');}"); Hello ();  hello,world//in strict mode, any variables or functions defined in the eval () are not accessed externally, and in strict mode, eval also causes errors. "User strict"; eval ("var str = ' Hello,nodejs ';"); alert (str); Error
<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > (3) Global Object properties: Undefined,nan,infinity, which are properties of the global object. </span>
In addition, all constructors of native types are also properties of the global object: object,function,array,number,boolean,string.

(4) Window object: All variables and functions declared in the global scope are properties of the Window object:
var name = "Xiaoming"; function Printname () {alert (window.name);} window.printname (); ' Xiaoming '





JavaScript stuff. (ii) Global object usage

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.