
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教程
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















































暂无评论内容