Javascript (2) Global object usage, javascriptglobal
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
<Span style = "font-family: Arial, Helvetica, sans-serif; background-color: rgb (255,255,255);"> (3) Global Object Attributes: undefined, NaN, infinity is the attribute of the Global object. </Span>
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'