JavaScript類型檢測的方法實例教程_第1頁
JavaScript類型檢測的方法實例教程_第2頁
JavaScript類型檢測的方法實例教程_第3頁
JavaScript類型檢測的方法實例教程_第4頁
JavaScript類型檢測的方法實例教程_第5頁
已閱讀5頁,還剩1頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第JavaScript類型檢測的方法實例教程1.typeof判斷基本類型

使用關鍵字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等類型結果均為object

2.instanceof判斷引用數據類型

instanceof利用的是變量的__proto__屬性指向原型的prototype屬性進行類型判斷,需要注意的是,如果對基本數據類型使用直接賦值的方法,則__proto__屬性是不存在的,我們需要使用構造函數。

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的實例,但它認為也是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的對象都包含一個內部屬性[[class]],這個屬性無法直接訪問,一般通過Ototype.toString()來查看。

如果toString方法沒有重寫的話,默認返回當前對象的[[Class]],其格式為[objectXxx],其中Xxx為對象的類型。但除了Object類型的對象外,其他類型直接使用toString方法時,會直接返回都是內容的字符串,所以我們需要使用call或者apply方法來改變toString方法的執行上下文。

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,但習慣上我們用null===null來判斷是否為null。

4.constructor判斷類型

constructor屬性會返回變量的構造函數,當然也可以利用字符串截取獲取構造函數名稱進行判斷來獲取布爾值,如"".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利用特征來判斷類型

在程序設計中,鴨子類型(英語:ducktyping)是動態類型的一種風格。在這種風格中,一個對象有效的語義,不是由繼承自特定的類或實現特定的接口,而是由"當前方法和屬性的集合"決定。

“當看到一只鳥走起來像鴨子、游泳起來像鴨子、叫起來也像鴨子,那么這只鳥就可以被稱為鴨子。”

在鴨子類型中,關注點在于對象的行為,能

溫馨提示

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

評論

0/150

提交評論