




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第JavaScript類型檢測的方法實例教程1.typeof判斷基本類型
使用關(guān)鍵字typeof返回的是類型名僅包括以下7種:number、string、boolean、undefined、symbol、object、function。
null和大部分的引用類型都不能用typeof進(jìn)行判斷。
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屬性進(jìn)行類型判斷,需要注意的是,如果對基本數(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進(jìn)行判斷。
注意:不同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ù)名稱進(jìn)行判斷來獲取布爾值,如"".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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 個人網(wǎng)上商店銷售平臺運營合作協(xié)議
- 返聘協(xié)議書書范本
- 建筑企業(yè)借款合同書
- 公司合并吸收轉(zhuǎn)讓協(xié)議書
- 生物醫(yī)藥市場分析與營銷試題
- 服裝店鋪協(xié)議書
- 月嫂定金協(xié)議書
- 軟件委托研發(fā)合同協(xié)議
- 通風(fēng)排煙施工合同協(xié)議
- 輕鋼工程分包合同協(xié)議
- ASME材料-設(shè)計許用應(yīng)力
- 采用SF6N2混合氣體絕緣的GIS母線和GIL應(yīng)用導(dǎo)則
- 數(shù)字貿(mào)易學(xué) 課件 第15章 數(shù)字支付與數(shù)字貨幣
- 中華民族共同體概論課件專家版6第六講 五胡入華與中華民族大交融(魏晉南北朝)
- 體外高頻熱療的護(hù)理
- JGJ79-2012 建筑地基處理技術(shù)規(guī)范
- ??低曅U性诰€測評題庫
- 新編酒水知識與調(diào)酒
- 采礦工程畢業(yè)設(shè)計(論文)-趙固二礦180萬ta新井設(shè)計
- XXX公司工程技術(shù)研究中心中心匯報
- 電網(wǎng)兩票培訓(xùn)課件
評論
0/150
提交評論