您现在的位置是:课程教程文章

js如何实现类型判断

2023-12-14 20:46课程教程文章 人已围观

1、判断引用类型和基本类型的类型是不同的,判断基本类型可以用typeof:

typeof1//'number'
typeof'1'//'string'
typeofundefined//'undefined'
typeoftrue//'boolean'
typeofSymbol()//'symbol'
typeofnull//'object'

2、typeof对引用类型,除了函数返回function,其他都返回object。但我们开发中数组肯定是要返回array类型的,所以typeof对引用类型来说并不是很适用。判断引用类型一般用instanceof:

varobj={}
vararr=[]
varfun=()=>{}
typeofobj//'object'
typeofarr//'object'
typeoffun//'function'
objinstanceofObject//true
arrinstanceofArray//true
funinstanceofFunction//true

以上就是js类型判断的实现,希望对大家有所帮助。更多js学习指路:js教程

推荐操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

课程教程:js如何实现类型判断

上一篇:一分钟告诉你动态ip和静态ip哪个好

下一篇:没有了

站点信息

  • 文章统计篇文章