这篇文章将为大家详细讲解有关使用JS如何实现判断数据类型,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
有五种数据判断类型方法typeof 、instanceof、constructor、Object.prototype.toString.call()、jquery.type()
一、typeof方法
typeof是个操作符,可以判断基本数据类型(返回的结果只能是number,string,boolean,null,symbol,function,object)
返回值分以下几种
对于基本类型。除了null值返回object以外,其他均返回正确的结果
对于引用值来说,除了function返回function类型,其他都返回object类型
例:
console.log( typeof 100, //"number" typeof 'abc', //"string" typeof false, //"boolean" typeof undefined, //"undefined" typeof null, //"object" typeof [1,2,3], //"object" typeof {a:1,b:2,c:3}, //"object" typeof function(){console.log('aaa');}, //"function" typeof new Date(), //"object" typeof /^[a-zA-Z]{5,20}$/, //"object" typeof new Error() //"object" typeof new Number(100), //'object' typeof new String('abc'),// 'string' typeof new Boolean(true),//'boolean' )
本文题目:使用JS如何实现判断数据类型-创新互联
文章链接:https://www.cdcxhl.com/article22/coidcc.html
成都网站建设公司_创新互联,为您提供网站收录、做网站、网站内链、云服务器、虚拟主机、外贸建站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联