window對(duì)象是Web瀏覽器中javascript的一個(gè)終極兜底兒對(duì)象,在作用域中處于最末端,是一個(gè)包含所有對(duì)象的對(duì)象。所有在全局作用域中定義的屬性和函數(shù)都是window對(duì)象的屬性
var myStringVar = 'myString';var myFunctionVar = function(){};console.log('myStringVar' in window);//trueconsole.log('myFunctionVar' in window);//true 1、引用
通常有兩種引用window對(duì)象的方法。第一種是簡(jiǎn)單引用賦予window對(duì)象的名稱;第二種是在全局作用域中使用this關(guān)鍵字
var foo ='bar';windowRef1 = window;windowRef2 = this;console.log(windowRef1,windowRef2);//輸出window對(duì)象的引用console.log(windowRef1.foo,windowRef2.foo);//'bar' 'bar'
2、特性
window對(duì)象是隱式的,通常不顯式引用;即使window對(duì)象顯式聲明,它也是隱式的,因?yàn)閣indow對(duì)象在作用域鏈中是最后一個(gè)
//window.alert()和alert()語(yǔ)句基本上是相同的var foo = {//window對(duì)象在這里是隱式的,window.foo fooMethod: function(){ alert('foo' + 'bar');//window對(duì)象在這里是隱式的,window.alert window.alert('foo' + 'bar');//顯式調(diào)用window對(duì)象,效果一樣 }}foo.fooMethod();//window對(duì)象在這里是隱式的,window.foo.fooMethod() 3、屬性
window對(duì)象共有18個(gè)屬性
undefined NaN Infinity Boolean String Number Object Array Function Date RegExp Error EvalError RangeError ReferenceError SyntaxError TypeError URIError
[注意]禁止給undefined、NaN和Infinity賦值
4、方法
Javascript附帶一些預(yù)定義函數(shù),被認(rèn)為是window對(duì)象的方法
1)、編碼方法
encodeURI():對(duì)整個(gè)URI進(jìn)行編碼,用特殊的UTF-8替換所有無(wú)效的字符
encodeURI()的不編碼字符有82個(gè):
! # $ & ' ( ) * + , - . / : ; = ? @ _ ~ 0-9 a-z A-Z
encodeURIComponent():對(duì)URI的某一段進(jìn)行編碼(常用于GET方法傳遞參數(shù)),用特殊的UTF-8替換所有無(wú)效的字符
一般來(lái)說(shuō),使用encodeURIComponent()比encodeURI()要多,因?yàn)樵趯?shí)踐中更常見(jiàn)的是查詢字符串參數(shù)而不是對(duì)基礎(chǔ)URI編碼。encodeURIComponent()的不編碼字符有71個(gè):
! ' ( ) * - . _ ~ 0-9 a-z A-Z
escape():對(duì)字符串進(jìn)行編碼,將字符的unicode編碼轉(zhuǎn)化為16進(jìn)制序列
ES3中反對(duì)escape()的使用,并建議用encodeURI和encodeURIComponent代替,不過(guò)escape()依然被廣泛的用于cookie的編碼,因?yàn)閑scape()恰好編碼了cookie中的非法字符并且對(duì)路徑中常出現(xiàn)的“/”不進(jìn)行編碼。escape()的不編碼字符有69個(gè):
* + - . / @ _ 0-9 a-z A-Z
decodeURI():解碼encodeURI()
decodeURIComponent():解碼encodeURIComponent()
unescape():解碼escape()
var uri = "http://www.wrox.com/illegal value.htm#start";console.log(encodeURI(uri));//http://www.wrox.com/illegal%20value.htm#startconsole.log(encodeURIComponent(uri));//http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23startconsole.log(escape(uri));//http%3A//www.wrox.com/illegal%20value.htm%23startvar uri = 'http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start';console.log(decodeURI(uri));//http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23startconsole.log(decodeURIComponent(uri));//http://www.wrox.com/illegal value.htm#startconsole.log(unescape(uri));//http://www.wrox.com/illegal value.htm#start
eval()
eval()方法像一個(gè)完整的ECMAScript解析器,只接受一個(gè)參數(shù),即要執(zhí)行的JavaScript字符串。當(dāng)解析器發(fā)現(xiàn)代碼中調(diào)用eval()方法時(shí),它會(huì)將傳入的參數(shù)當(dāng)作實(shí)際的ECMAScript語(yǔ)句來(lái)解析,然后把執(zhí)行結(jié)果插入到原位置。eval()方法能夠解釋字符串的能力非常強(qiáng)大,但也非常危險(xiǎn)。當(dāng)用它執(zhí)行用戶輸入數(shù)據(jù)時(shí),可能會(huì)有惡意用戶輸入威脅站點(diǎn)或應(yīng)用程序字符的代碼,就是所謂的代碼注入
[注意]在嚴(yán)格模式下,外部訪問(wèn)不到eval()中創(chuàng)建的任何變量或函數(shù),為eval賦值也會(huì)導(dǎo)致錯(cuò)誤
2)、數(shù)字方法
Window對(duì)象下還有isFinite()、isNaN()、parseFloat()、parseInt()這四個(gè)關(guān)于數(shù)字的方法
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。



















