js如何实现类型判断

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教程

原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容