测试一下全局对象的部分属性和方法

 

*Gobal内容

---属性

NaN, 表示非数字

Infinity, 表示无限大数字

debugger, 还不知道如何使用?

undefined , 表示一个实例没有定义任何值,是一个特殊的基本类型

---方法

eval, 执行一段string格式的javascript脚本, 将执行结果放在eval位置上.(可能需要一篇专门的文章讨论其执行作用域问题).

parseInt, (详见,http://www.w3schools.com/jsref/jsref_parseint.asp)

parseFloat, (详见, http://www.w3school.com.cn/js/jsref_parseFloat.asp)

escape,unescape,(ECMA不推荐,见http://www.w3school.com.cn/js/jsref_escape.asp)

isNaN, if 非数字 return true, 否则 false

isFinite, if InFinity return false, 否则 true

decodeURI,encodeURI (字母数字和属于URI特殊字符的保持原样, 其他的字符编码,如空格替换为'%20')

decodeURIComponent,encodeURIComponent (编码所有非字母数字的字符,使用时需注意,否则url非法)

 

*示例

---源码 (在Node中测试)

t_global.js
 1 //attribute
 2 console.log("//test NaN");
 3 if(NaN)
 4     console.log("NaN is true");
 5 else
 6     console.log("NaN is false");
 7 console.log("NaN's value: " + NaN);
 8 console.log("NaN's type: " + NaN);
 9 console.log("\n");
10 
11 console.log("//test Infinity");
12 if(Infinity)
13     console.log("Infinity is true");
14 else
15     console.log("Infinity is false");
16 console.log("Infinity's value: " + Infinity);
17 console.log("Infinity's type: " + Infinity);
18 console.log("\n");
19 
20 console.log("//test undefined");
21 if(undefined)
22     console.log("undefined is true");
23 else
24     console.log("undefined is false");
25 console.log("undefined's value: " + undefined);
26 console.log("undefined's type: " + undefined);
27 console.log("\n");
28 
29 /**
30 console.log("//test debugger");
31 if(debugger)
32     console.log("debugger is true");
33 else
34     console.log("debugger is false");
35 console.log("debugger's value: " + debugger);
36 console.log("debugger's type: " + debugger);
37 console.log("\n");
38 */
39 
40 //function
41 console.log("//test escape/unescape");
42 var str =  escape("dai zhong");
43 console.log("escape(string): " + str);
44 console.log("unescape(string): " + unescape(str));
45 console.log("\n");
46 
47 console.log("//test encodeURI/decodeURI");
48 var url = "http://www.cnblogs.com/bronte 戴忠的博客";
49 url = encodeURI(url);
50 console.log("encodeURI: " + url);
51 console.log("decodeURI: " + decodeURI(url));
52 console.log("\n");
53 
54 console.log("//test encodeURIComponent/decodeURIComponent");
55 var url = "http://www.cnblogs.com/bronte 戴忠的博客";
56 url = encodeURIComponent(url);
57 console.log("encodeURIComponent: " + url);
58 console.log("decodeURIComponent: " + decodeURIComponent(url));
59 console.log("\n");
60 
61 console.log("//test eval");
62 var str = "eval";
63 eval("console.log(str)"); // eval
64 console.log("\n");
65 
66 console.log("//test isFinite");
67 if(isFinite(Infinity))
68     console.log("isFinite(Infinity) is true");
69 else
70     console.log("isFinite(Infinity) is false");
71 if(isFinite(11))
72     console.log("isFinite(11) is true");
73 else
74     console.log("isFinite(11) is false");
75 console.log("\n");

 

---结果

测试结果
 1 //test NaN
 2 NaN is false
 3 NaN's value: NaN
 4 NaN's type: NaN
 5 
 6 
 7 //test Infinity
 8 Infinity is true
 9 Infinity's value: Infinity
10 Infinity's type: Infinity
11 
12 
13 //test undefined
14 undefined is false
15 undefined's value: undefined
16 undefined's type: undefined
17 
18 
19 //test escape/unescape
20 escape(string): dai%20zhong
21 unescape(string): dai zhong
22 
23 
24 //test encodeURI/decodeURI
25 encodeURI: http://www.cnblogs.com/bronte%20%E6%88%B4%E5%BF%A0%E7%9A%84%E5%8D%9A%E5%AE%A2
26 decodeURI: http://www.cnblogs.com/bronte 戴忠的博客
27 
28 
29 //test encodeURIComponent/decodeURIComponent
30 encodeURIComponent: http%3A%2F%2Fwww.cnblogs.com%2Fbronte%20%E6%88%B4%E5%BF%A0%E7%9A%84%E5%8D%9A%E5%AE%A2
31 decodeURIComponent: http://www.cnblogs.com/bronte 戴忠的博客
32 
33 
34 //test eval
35 eval
36 
37 
38 //test isFinite
39 isFinite(Infinity) is false
40 isFinite(11) is true

  

*总结

---全局对象是一个特殊的内置对象, 由解析器在初始化时创建, 具体如何实现有具体环境决定, 如浏览器中的window对象就是全局对象;

---全局对象表示的是所有对象的根对象, 比如js的语法之一,name = "dzh",这个name没有用var声明, 那么name被添加到了全局对象的属性上;

---所以它提供了最为常用的功能和属性定义.

 

*参考

http://www.w3school.com.cn/js/jsref_obj_global.asp

posted on 2012-06-12 03:25  戴忠  阅读(4330)  评论(0编辑  收藏  举报