Javascript (2) Global object usage

Source: Internet
Author: User

Javascript (2) Global object usage
1. Global Object: All attributes and methods defined in the Global scope are Global object attributes.
2. The Global object is not directly used and cannot be created using the new operator. It is created when the Scripting engine is initialized and made available even if its method and attributes are available.
For example, isNaN, isFinite (), parseInt (), and parseFloat () are all Global object methods.
The following describes several important methods.

(1) URI encoding and decoding: encodeURI () \ encodeURIComponent (), decodeURI \ decodeURIComponent ()
EncodeURI () and encodeURIComponent () are used to encode the URI (common resource identifier) and send it to the browser.
The browser cannot understand and receive special characters, such as space, so the two methods replace invalid characters in the URI with UTF-8 encoding for the browser to recognize.
EncodeURI (): it is mainly used for the entire URI and does not encode special characters belonging to the URI, such as colons, forward slashes, question marks, and well numbers.
EncodeURIComponent (): it is mainly used to encode a section of URI and encode any non-standard characters it discovers.

Var uri = "http://www.baidu.com/aa value # query"; // console is replaced with only spaces. log (encodeURI (uri); // output: uri (encodeURIComponent (uri); // output: http % 3A % 2F % 2Fwww.baidu.com % 2Faa % 20 value % 23 query // best practices: // encodeURI: Use // encodeURIComponent () for the entire URI (): use /// decodeURI () to decode the characters encoded with encodeURI. // decodeURIComponent (): decodes 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 % 20 value % 23 query"; 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, that is, a javascript string, similar to an ECMAScript Parser:

Eval ("alert ('hello, nodejs')"); // equivalent to alert ('hello, nodejs'); // In eval () the executed code is considered to be part of the execution environment of the call. 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, no variables or functions defined in eval () can be accessed externally. Similarly, in strict mode, for eval, errors may also occur. "User strict"; eval ("var str = 'hello, nodejs';"); alert (str); // Error
(3) attributes of the Global Object: undefined, NaN, and Infinity are all attributes of the Global object.
In addition, all native constructors are Global Object attributes: Object, Function, Array, Number, Boolean, String.

(4) window object: all variables and functions declared in the global scope are the properties of the window object:
 var name = "xiaoming"; function printName(){ alert(window.name); } window.printName(); //'xiaoming'





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.