【JS】引用类型之Global

Global是一个固有对象(全局),不属于任何对象,目的是把所有全局方法集中在一个对象中。

这里挑几个常用的global方法

1、URI编码解码方法

编码(encodeURI/encodeURIComponent)

var uri = "http://www.wrox.com/illegal value.htm#start";

//"http://www.wrox.com/illegal%20value.htm#start"
alert(encodeURI(uri));

//"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"
alert(encodeURIComponent(uri));

解码(decodeURI/decodeURIComponent)

var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start";

//http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start
alert(decodeURI(uri));

//http://www.wrox.com/illegal value.htm#start
alert(decodeURIComponent(uri));

2、eval()

该方法相当于一个JS解析器,能够执行传入的任何JS代码

eval("alert('hello')");
//等价于alert('hello');

eval("function sayHello(){alert('hello');}");
sayHello();//hello

//将一个字符串转换成一个对象
eval("var obj=" + response.responseText);
alert(obj.xxxx);

 

window对象既是Global的属性,同时它也是一个Global对象

 

posted @ 2013-04-11 16:09  自行车上的程序员  阅读(1519)  评论(0编辑  收藏  举报