JavaScript判断数据类型有几种方法及区别介绍
有五种数据判断类型方法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(
typeof100,//"number"
typeof'abc',//"string"
typeoffalse,//"boolean"
typeofundefined,//"undefined"
typeofnull,//"object"
typeof[1,2,3],//"object"
typeof{a:1,b:2,c:3},//"object"
typeoffunction(){console.log('aaa');},//"function"
typeofnewDate(),//"object"
typeof/^[a-zA-Z]{5,20}$/,//"object"
typeofnewError()//"object"
typeofnewNumber(100),//'object'
typeofnewString('abc'),//'string'
typeofnewBoolean(true),//'boolean'
)
二、instanceof方法
一般用来检测引用数据类型,表达式为:AinstanceofB,判断A是否是B的实例,如果A是B的实例,则返回true,否则返回false,由构造类型判断出数据类型
console.log(
100instanceofNumber,//false
'dsfsf'instanceofString,//false
falseinstanceofBoolean,//false
undefinedinstanceofObject,//false
nullinstanceofObject,//false
[1,2,3]instanceofArray,//true
{a:1,b:2,c:3}instanceofObject,//true
function(){console.log('aaa');}instanceofFunction,//true
newDate()instanceofDate,//true
/^[a-zA-Z]{5,20}$/instanceofRegExp,//true
newError()instanceofError//true
)
//注意:instanceof后面一定要是对象类型,大小写不能写错,该方法试用一些条件选择或分支
还需要注意null和undefined都返回了false,这是因为它们的类型就是自己本身,并不是Object创建出来它们,所以返回了false。
三、constructor方法
constructor是prototype对象上的属性,指向构造函数,
varnum=123;
varstr='abcdef';
varbool=true;
vararr=[1,2,3,4];
varjson={name:'wenzi',age:25};
varfunc=function(){console.log('thisisfunction');}
varund=undefined;
varnul=null;
vardate=newDate();
varreg=/^[a-zA-Z]{5,20}$/;
varerror=newError();
functionPerson(){
}
vartom=newPerson();
//undefined和null没有constructor属性
console.log(
tom.constructor==Person,
num.constructor==Number,
str.constructor==String,
bool.constructor==Boolean,
arr.constructor==Array,
json.constructor==Object,
func.constructor==Function,
date.constructor==Date,
reg.constructor==RegExp,
error.constructor==Error
);
//所有结果均为true
注意:除了undefined和null之外,其他类型都可以通过constructor属性来判断类型
方法四:Object.prototype.toString方法
用来检测对象类型
vartoString=Object.prototype.toString;
toString.call(123);//"[objectNumber]"
toString.call('abcdef');//"[objectString]"
toString.call(true);//"[objectBoolean]"
toString.call([1,2,3,4]);//"[objectArray]"
toString.call({name:'wenzi',age:25});//"[objectObject]"
toString.call(function(){console.log('thisisfunction');});//"[objectFunction]"
toString.call(undefined);//"[objectUndefined]"
toString.call(null);//"[objectNull]"
toString.call(newDate());//"[objectDate]"
toString.call(/^[a-zA-Z]{5,20}$/);//"[objectRegExp]"
toString.call(newError());//"[objectError]"
toString是Object原型对象上的一个方法,该方法默认返回其调用者的具体类型更严格的讲,是toString运行时this指向的对象类型,返回的类型格式为[object,xxx],xxx是具体的数据类型,其中包括:String,Number,Boolean,Undefined,Null,Function,Date,Array,RegExp,Error,HTMLDocument等等都可以通过这个方法获取到
5、无敌万能的方法:jquery.type()
如果对象是undefined或null,则返回相应的“undefined”或“null”。
jQuery.type(undefined)==="undefined" jQuery.type()==="undefined" jQuery.type(window.notDefined)==="undefined" jQuery.type(null)==="null"
如果对象有一个内部的[[Class]]和一个浏览器的内置对象的[[Class]]相同,我们返回相应的[[Class]]名字
jQuery.type(true)==="boolean"
jQuery.type(3)==="number"
jQuery.type("test")==="string"
jQuery.type(function(){})==="function"
jQuery.type([])==="array"
jQuery.type(newDate())==="date"
jQuery.type(newError())==="error"//asofjQuery1.9
jQuery.type(/test/)==="regexp"
其他一切都将返回它的类型“object”。
6.自己也可以封装一个获取变量准确类型的函数
functiongettype(obj){
vartype=typeofobj;
if(type!=='object'){
returntype;
}
//如果不是object类型的数据,直接用typeof就能判断出来
//如果是object类型数据,准确判断类型必须使用Object.prototype.toString.call(obj)的方式才能判断
returnObject.prototype.toString.call(obj).replace(/^\[object(\S+)\]$/,'$1');
}
总结
到此这篇关于JavaScript判断数据类型有几种方法及区别介绍的文章就介绍到这了,更多相关js判断数据类型内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。