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(
 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'
)
 

二、instanceof方法

一般用来检测引用数据类型,表达式为:A instanceof B,判断A是否是B的实例,如果 A 是 B 的实例,则返回 true,否则返回 false,由构造类型判断出数据类型

console.log(
 100 instanceof Number, //false
 'dsfsf' instanceof String, //false
 false instanceof Boolean, //false
 undefined instanceof Object, //false
 null instanceof Object, //false
 [1,2,3] instanceof Array, //true
 {a:1,b:2,c:3} instanceof Object, //true
 function(){console.log('aaa');} instanceof Function, //true
 new Date() instanceof Date, //true
 /^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
 new Error() instanceof Error //true
)
//注意: instanceof 后面一定要是对象类型,大小写不能写错,该方法试用一些条件选择或分支

还需要注意null和undefined都返回了false,这是因为它们的类型就是自己本身,并不是Object创建出来它们,所以返回了false。

三、constructor方法

constructor是prototype对象上的属性,指向构造函数,

var num = 123;
var str = 'abcdef';
var bool = true;
var arr = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und = undefined;
var nul = null;
var date = new Date();
var reg = /^[a-zA-Z]{5,20}$/;
var error= new Error();

function Person(){
 
}
var tom = new Person();

// 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 方法

用来检测对象类型

var toString = Object.prototype.toString;

toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({name:'wenzi', age:25}); //"[object Object]"
toString.call(function(){ console.log('this is function'); }); //"[object Function]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"

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( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"

其他一切都将返回它的类型“object”。
6 . 自己也可以封装一个获取变量准确类型的函数

function gettype(obj) {
 var type = typeof obj;

 if (type !== 'object') {
 return type;
 }
 //如果不是object类型的数据,直接用typeof就能判断出来

 //如果是object类型数据,准确判断类型必须使用Object.prototype.toString.call(obj)的方式才能判断
 return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1');
}

总结

到此这篇关于JavaScript判断数据类型有几种方法及区别介绍的文章就介绍到这了,更多相关js 判断数据类型内容请搜索来客网以前的文章或继续浏览下面的相关文章希望大家以后多多支持来客网!