Home >Web Front-end >JS Tutorial >Javascript Global Object_Basic Knowledge
Global对象
Global对象是ECMAScript中最特别的对象,因为实际上它根本不存在。如果尝试编写下面的代码,将得到错误:
错误消息显示Global不是对象,但刚才不是说Global是对象吗?没错。这里需要理解的主要概念是,在ECMAScript中,不存在独立的函数,所有函数都必须是某个对象的方法。本书前面介绍的函数,如isNaN()、isFinite()、parseInt()和parseFloat()等,看起来都像独立的函数。实际上,它们都是Global对象的方法。而且Global对象的方法不止这些。
encodeURI()和encodeURIComponent()方法用于编码传递给浏览器的URI(统一资源标识符)。有效的URI不能包含某些字符,如空格。这两个方法用于编码URI,这样用专门的UTF-8编码替换所有的非有效字符,就可以使浏览器仍能够接受并理解它们。
encodeURI()方法用于处理完整的URI(例如,http://www.wrox.com/illegal value.htm),而encodeURIComponent()用于处理URI的一个片断(如前面的URI中的illegal value.htm)。这两个方法的主要区别是encodeURI()方法不对URI中的特殊字符进行编码,如冒号、前斜杠、问号和英镑符号,而encodeURIComponent()则对它发现的所有非标准字符进行编码。例如:
这段代码输出两个值:
可以看到,除空格外,第一个URI无任何改变,空格被替换为%20。第二个URI中的所有非字母数字字符都被替换成它们对应的编码,基本上使这个URI变得无用。这就是encodeURI()可以处理完整URI,而encodeURIComponent()只能处理附加在已有URI末尾的字符串的原因。
自然,还有两个方法用于解码编码过的URI,即decodeURI()和decodeURIComponent()。如你所料,这两个方法所做的恰与其对应的方法相反。decodeURI()方法只对用encodeURI()方法替换的字符解码。例如,%20将被替换为空格,而%23不会被替换,因为它表示的是英镑符号(#),encodeURI()并不替换这个符号。同样的,decodeURIComponent()会解码所有encodeURIComponent()编码过的字符,意味着它将对所有的特殊值解码。例如:
这段代码输出两个值:
在这个例子中,变量uri存放的是用encodeURIComponent()编码的字符串。生成的值说明了应用两个解码方法时会发生的事情。第一个值由decodeURI()输出,把%20替换成空格。第二个值由decodeURIComponent()输出,替换所有的特殊。
这些URI方法encodeURI()、encodeURIComponent()、decodeURI()和decodeURICom- ponent()代替了BOM的escape()和unescape()方法。URI方法更可取,因为它们会对所有Unicode符号编码,而BOM方法只能对ASCII符号正确编码。尽量避免使用escape()和unescape()方法。
The last method is probably the most powerful method in the entire ECMAScript language, the eval() method. This method is like the entire ECMAScript interpreter, accepting one parameter, which is the ECMAScript (or JavaScript) string to be executed. For example:
This line of code is functionally equivalent to the following code:
When the interpreter finds an eval() call, it will interpret the argument as a real ECMAScript statement and insert it where the function is. This means that variables referenced inside the eval() call can be defined outside of the parameters:
Here, the variable msg is defined outside the context of the eval() call, and the warning still displays the text "hello world" because the second line of code will be replaced with a real line of code. Likewise, a function or variable can be defined inside an eval() call and then referenced in code outside the function:
Here, the function sayHi() is defined inside the eval() call. Because the call will be replaced with a real function, sayHi() can still be called on the next line.
This function is very powerful, but also very dangerous. Be extremely careful when using eval(), especially when passing it user-entered data. A malicious user may insert values that compromise the security of the site or application (called code injection).
Global object not only has methods, it also has properties. 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 explains all properties of the Global object in more detail:
Attributes
|
Say Ming |
||||||||||||||||||||||||||||||||||||||
undefined | Literal of Undefined type | ||||||||||||||||||||||||||||||||||||||
NaN | Special numerical values that are not numbers | ||||||||||||||||||||||||||||||||||||||
Infinity | Special numerical value for infinity | ||||||||||||||||||||||||||||||||||||||
Object | Constructor of Object | ||||||||||||||||||||||||||||||||||||||
Array | Constructor of Array | ||||||||||||||||||||||||||||||||||||||
Function | Constructor of Function | ||||||||||||||||||||||||||||||||||||||
Boolean | Boolean constructor | ||||||||||||||||||||||||||||||||||||||
String | Constructor of String | ||||||||||||||||||||||||||||||||||||||
Number | Number’s constructor | ||||||||||||||||||||||||||||||||||||||
Date | Date’s constructor | ||||||||||||||||||||||||||||||||||||||
RegExp | Constructor of RegExp | ||||||||||||||||||||||||||||||||||||||
Error | Constructor of Error | ||||||||||||||||||||||||||||||||||||||
EvalError | Constructor of EvalError | ||||||||||||||||||||||||||||||||||||||
RangeError | Constructor of RangeError | ||||||||||||||||||||||||||||||||||||||
ReferenceError | Constructor of ReferenceError | ||||||||||||||||||||||||||||||||||||||
SyntaxError | Constructor of SyntaxError | ||||||||||||||||||||||||||||||||||||||
TypeError | Constructor of TypeError | ||||||||||||||||||||||||||||||||||||||
URIError | Constructor of URIError |