




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
第JavaScript類型檢測的方法實例教程1.typeof判斷基本類型
使用關(guān)鍵字typeof返回的是類型名僅包括以下7種:number、string、boolean、undefined、symbol、object、function。
null和大部分的引用類型都不能用typeof進行判斷。
letnum=32
letstr="32"
letbool=true
letnul=null
letundef=undefined
letsym=Symbol()
constobj=newObject()
constarr=newArray()
constfun=newFunction()
constdate=newDate()
constreg=newRegExp()
console.log(typeofnum)//number
console.log(typeofstr)//string
console.log(typeofbool)//boolean
console.log(typeofnul)//object
console.log(typeofundef)//undefined
console.log(typeofsym)//symbol
console.log(typeofobj)//object
console.log(typeofarr)//object
console.log(typeoffun)//function
console.log(typeofdate)//object
console.log(typeofreg)//object
注意:用typeof判斷null、Array、Date、RegExp等類型結(jié)果均為object
2.instanceof判斷引用數(shù)據(jù)類型
instanceof利用的是變量的__proto__屬性指向原型的prototype屬性進行類型判斷,需要注意的是,如果對基本數(shù)據(jù)類型使用直接賦值的方法,則__proto__屬性是不存在的,我們需要使用構(gòu)造函數(shù)。
constobj=newObject()
constarr=newArray()
constfun=newFunction()
constdate=newDate()
constreg=newRegExp()
console.log(objinstanceofObject)//true
console.log(arrinstanceofArray)//true
console.log(funinstanceofFunction)//true
console.log(dateinstanceofDate)//true
console.log(reginstanceofRegExp)//true
letnum1=32
letnum2=newNumber(32)
console.log(num1instanceofNumber)//false
console.log(num2instanceofNumber)//true
另外,雖然instanceof能夠判斷出arr是Array的實例,但它認(rèn)為也是Object的實例,這對判斷一個未知引用類型并不友好。
constarr=newArray()
console.log(arrinstanceofArray)//true
console.log(arrinstanceofObject)//true
原因是arr.__proto__的__proto__屬性指向Object的原型對象。
對于這種情況,可以換用constructor進行判斷。
注意:不同window或iframe間的對象檢測不能使用instanceof!
3.Ototype.toString判斷類型
toString()是Object的原型方法,每一個繼承Object的對象都有toString方法。
所有使用typeof返回值為object的對象都包含一個內(nèi)部屬性[[class]],這個屬性無法直接訪問,一般通過Ototype.toString()來查看。
如果toString方法沒有重寫的話,默認(rèn)返回當(dāng)前對象的[[Class]],其格式為[objectXxx],其中Xxx為對象的類型。但除了Object類型的對象外,其他類型直接使用toString方法時,會直接返回都是內(nèi)容的字符串,所以我們需要使用call或者apply方法來改變toString方法的執(zhí)行上下文。
letnum=32
letstr="32"
letbool=true
letnul=null
letundef=undefined
letsym=Symbol()
constobj=newObject()
constarr=newArray()
constfun=newFunction()
constdate=newDate()
constreg=newRpgExp()
console.log(Ototype.toString.apply(num))//"[objectNumber]"
console.log(Ototype.toString.apply(str))//"[objectString]"
console.log(Ototype.toString.apply(bool))//"[objectBoolean]"
console.log(Ototype.toString.apply(nul))//"[objectNull"
console.log(Ototype.toString.apply(undef))//"[objectUndefined]"
console.log(Ototype.toString.apply(sym)//"[objectSymbol]"
console.log(Ototype.toString.call(obj))//"[objectObject]"
console.log(Ototype.toString.call(arr))//"[objectArray]"
console.log(Ototype.toString.call(fun))//"[objectFunction]"
console.log(Ototype.toString.call(date))//"[objectDate]"
console.log(Ototype.toString.call(reg)//"[objectRegExp]"
Ototype.toString可以判斷null,但習(xí)慣上我們用null===null來判斷是否為null。
4.constructor判斷類型
constructor屬性會返回變量的構(gòu)造函數(shù),當(dāng)然也可以利用字符串截取獲取構(gòu)造函數(shù)名稱進行判斷來獲取布爾值,如"".constructor===String。
letnum=32
letstr="32"
letbool=true
letnul=null
letundef=undefined
letsym=Symbol()
constobject=newObject()
constarr=newArray()
constfun=newFunction()
constdate=newDate()
constreg=newRegExp()
console.log(num.constructor)//Number(){[nativecode]}
console.log(str.constructor)//String(){[nativecode]}
console.log(bool.constructor)//Boolean(){[nativecode]}
console.log(nul.constructor)//UncaughtTypeError:Cannotreadproperty'constructor'ofnull
console.log(undef.constructor)//UncaughtTypeError:Cannotreadproperty'constructor'ofundefined
console.log(sym.constructor)//Symbol(){[nativecode]}
console.log(obj.constructor===Object)//true
console.log(arr.constructor===Array)//true
console.log(fun.constructor===Function)//true
console.log(date.constructor===Date)//true
console.log(reg.constructor===RegExp)//true
無法用constructor判斷null和undefined,但可以避免使用instanceof時arr的原型對象既可以為Array也可以是Object。
5.ducktype利用特征來判斷類型
在程序設(shè)計中,鴨子類型(英語:ducktyping)是動態(tài)類型的一種風(fēng)格。在這種風(fēng)格中,一個對象有效的語義,不是由繼承自特定的類或?qū)崿F(xiàn)特定的接口,而是由"當(dāng)前方法和屬性的集合"決定。
“當(dāng)看到一只鳥走起來像鴨子、游泳起來像鴨子、叫起來也像鴨子,那么這只鳥就可以被稱為鴨子。”
在鴨子類型中,關(guān)注點在于對象的行為,能
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 暑期俱樂部課堂活動方案
- 月度團隊活動方案
- 晨會特長展示活動方案
- 暑假親子拍攝活動方案
- 村級走訪脫貧戶活動方案
- 村委會節(jié)日走訪活動方案
- 村級冬至活動方案
- 村姐妹聚會活動方案
- 杏林聯(lián)誼活動方案
- 林志玲助學(xué)活動方案
- GB/T 40998-2021變性淀粉中羥丙基含量的測定分光光度法
- GB/T 3672.2-2002橡膠制品的公差第2部分:幾何公差
- GB/T 31848-2015汽車貼膜玻璃貼膜要求
- GB/T 25146-2010工業(yè)設(shè)備化學(xué)清洗質(zhì)量驗收規(guī)范
- GB/T 18884.2-2015家用廚房設(shè)備第2部分:通用技術(shù)要求
- GB/T 12239-2008工業(yè)閥門金屬隔膜閥
- 行政法培訓(xùn)講義課件
- DB32T 4174-2021 城市居住區(qū)和單位綠化標(biāo)準(zhǔn)
- 基本原理與性能特點多自由度電磁軸承課件
- 北京輸變電工程標(biāo)準(zhǔn)工藝應(yīng)用圖冊(圖文并茂)
- 三相負(fù)荷(380V)及單相(220V)最大供電距離計算表及電壓降計算表
評論
0/150
提交評論