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

提交评论