JavaScript判断数据类型的五种方法及区别详解
本文详细介绍JavaScript中判断数据类型的五种常用方法:typeof、instanceof、constructor、Object.prototype.toString以及jquery.type。分析各方法的适用场景、返回值特点及局限性,特别是typeof对null和引用类型的处理缺陷,以及Object.prototype.toString在精确类型判断中的优势,帮助开发者根据实际需求选择合适的数据类型检测方案。
JavaScript数据类型判断方法概述
在JavaScript开发中,判断变量数据类型是常见需求。目前主要存在五种判断方法:typeof、instanceof、constructor、Object.prototype.toString.call()以及jquery.type()。
typeof运算符的使用与局限
一、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检测引用类型
二、instanceof方法
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使用instanceof检测时均返回false。这是因为它们的类型即为自身,并非由Object构造函数创建,因此无法通过instanceof判定为Object实例。
constructor属性判断类型
三、constructor方法
constructor是原型对象上的属性,指向构造函数。
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精确检测
方法四: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等均可通过此方法获取。
jquery.type()与自定义封装
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判断数据类型的内容,可搜索本站以往文章或继续浏览相关文章。感谢支持!
JS split切割后出现空字符串怎么过滤
2026-11-05 16:00:30
在 React 或 Vue 项目中封装使用 JS 节流防抖
2026-11-05 15:27:29
JavaScript中怎么将带小数的字符串转为整数?
2026-11-05 14:32:38
怎么用JavaScript策略模式重构电商促销计算逻辑?
2026-11-05 14:01:09
JS使用constructor判断数据类型可靠吗
2026-11-05 13:49:41
