JavaScript類型檢測(cè)的方法實(shí)例教程_第1頁(yè)
JavaScript類型檢測(cè)的方法實(shí)例教程_第2頁(yè)
JavaScript類型檢測(cè)的方法實(shí)例教程_第3頁(yè)
JavaScript類型檢測(cè)的方法實(shí)例教程_第4頁(yè)
JavaScript類型檢測(cè)的方法實(shí)例教程_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第JavaScript類型檢測(cè)的方法實(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)行類型判斷,需要注意的是,如果對(duì)基本數(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í)例,這對(duì)判斷一個(gè)未知引用類型并不友好。

constarr=newArray()

console.log(arrinstanceofArray)//true

console.log(arrinstanceofObject)//true

原因是arr.__proto__的__proto__屬性指向Object的原型對(duì)象。

對(duì)于這種情況,可以換用constructor進(jìn)行判斷。

注意:不同window或iframe間的對(duì)象檢測(cè)不能使用instanceof!

3.Ototype.toString判斷類型

toString()是Object的原型方法,每一個(gè)繼承Object的對(duì)象都有toString方法。

所有使用typeof返回值為object的對(duì)象都包含一個(gè)內(nèi)部屬性[[class]],這個(gè)屬性無(wú)法直接訪問(wèn),一般通過(guò)Ototype.toString()來(lái)查看。

如果toString方法沒(méi)有重寫的話,默認(rèn)返回當(dāng)前對(duì)象的[[Class]],其格式為[objectXxx],其中Xxx為對(duì)象的類型。但除了Object類型的對(duì)象外,其他類型直接使用toString方法時(shí),會(huì)直接返回都是內(nèi)容的字符串,所以我們需要使用call或者apply方法來(lái)改變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來(lái)判斷是否為null。

4.constructor判斷類型

constructor屬性會(huì)返回變量的構(gòu)造函數(shù),當(dāng)然也可以利用字符串截取獲取構(gòu)造函數(shù)名稱進(jìn)行判斷來(lái)獲取布爾值,如"".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

無(wú)法用constructor判斷null和undefined,但可以避免使用instanceof時(shí)arr的原型對(duì)象既可以為Array也可以是Object。

5.ducktype利用特征來(lái)判斷類型

在程序設(shè)計(jì)中,鴨子類型(英語(yǔ):ducktyping)是動(dòng)態(tài)類型的一種風(fēng)格。在這種風(fēng)格中,一個(gè)對(duì)象有效的語(yǔ)義,不是由繼承自特定的類或?qū)崿F(xiàn)特定的接口,而是由"當(dāng)前方法和屬性的集合"決定。

“當(dāng)看到一只鳥走起來(lái)像鴨子、游泳起來(lái)像鴨子、叫起來(lái)也像鴨子,那么這只鳥就可以被稱為鴨子。”

在鴨子類型中,關(guān)注點(diǎn)在于對(duì)象的行為,能

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論