




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第JavaScript類型檢測的方法實(shí)例教程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的實(shí)例,但它認(rèn)為也是Object的實(shí)例,這對判斷一個未知引用類型并不友好。
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)看到一只鳥走起來像鴨子、游泳起來像鴨子、叫起來也像鴨子,那么這只鳥就可以被稱為鴨子?!?/p>
在鴨子類型中,關(guān)注點(diǎ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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 環(huán)氧樹脂-碳纖維復(fù)合材料行業(yè)深度調(diào)研及發(fā)展項(xiàng)目商業(yè)計劃書
- 兒童專屬無添加果茶企業(yè)制定與實(shí)施新質(zhì)生產(chǎn)力項(xiàng)目商業(yè)計劃書
- 高效能光譜分析儀行業(yè)深度調(diào)研及發(fā)展項(xiàng)目商業(yè)計劃書
- 高精度稱重傳感器行業(yè)跨境出海項(xiàng)目商業(yè)計劃書
- 高精度溫度控制器企業(yè)制定與實(shí)施新質(zhì)生產(chǎn)力項(xiàng)目商業(yè)計劃書
- 高效能濾波器企業(yè)制定與實(shí)施新質(zhì)生產(chǎn)力項(xiàng)目商業(yè)計劃書
- 住宿業(yè)用戶體驗(yàn)提升行業(yè)跨境出海項(xiàng)目商業(yè)計劃書
- 生物打印技術(shù)在工程中的應(yīng)用行業(yè)深度調(diào)研及發(fā)展項(xiàng)目商業(yè)計劃書
- 互聯(lián)網(wǎng)消費(fèi)金融分期行業(yè)跨境出海項(xiàng)目商業(yè)計劃書
- 小學(xué)四年級數(shù)學(xué)期末試卷2
- 中國真正丹道理法及工程次第闡真
- 2022年四川省成都市中考英語試卷及答案
- 商務(wù)英語寫作實(shí)踐智慧樹知到答案章節(jié)測試2023年中北大學(xué)
- 新年春節(jié)廉潔過年過廉潔年端午節(jié)清廉文化中秋節(jié)廉潔過節(jié)優(yōu)秀課件兩篇
- GB/T 10920-2008螺紋量規(guī)和光滑極限量規(guī)型式與尺寸
- 認(rèn)知宇宙飛船之星際探索
- 皮膚病理知識學(xué)習(xí)整理課件整理
- 人工智能課件213產(chǎn)生式表示法
- 空調(diào)維保質(zhì)量保障體系及措施方案
- 建筑樁基技術(shù)規(guī)范2018
- 信息隱藏與數(shù)字水印課件(全)全書教學(xué)教程完整版電子教案最全幻燈片
評論
0/150
提交評論