JavaScript类型检测的4种方法详解:typeof、toString、constructor与instanceof
本文详解JavaScript中检测数据类型的4种常用方法。分析typeof的局限性与基础用法,介绍Object.prototype.toString的精准识别能力,探讨constructor属性的自定义对象支持,以及instanceof对原型链的判断机制。通过对比各方法的输出结果、功能范围及报错情况,帮助开发者根据场景选择最合适的类型检测方案。
本文详细介绍JavaScript中用于判断数据类型的4种常用方法。
typeof运算符检测
1. typeof运算符
【输出结果】返回首字母小写的字符串形式。
【功能特点】
[a] 能够识别基本数据类型(注意:null会被识别为object) [b] 无法区分具体的对象类型(Function除外)
【代码示例】
console.log(typeof "jerry");//"string"
console.log(typeof 12);//"number"
console.log(typeof true);//"boolean"
console.log(typeof undefined);//"undefined"
console.log(typeof null);//"object"
console.log(typeof {name: "jerry"});//"object"
console.log(typeof function(){});//"function"
console.log(typeof []);//"object"
console.log(typeof new Date);//"object"
console.log(typeof /\d/);//"object"
function Person(){};
console.log(typeof new Person);//"object"
Object.prototype.toString方法
2. Object.prototype.toString方法
【输出结果】返回[object 数据类型]格式的字符串。
【功能特点】
[a] 能够识别基本数据类型及内置对象类型 [b] 无法识别自定义对象类型(均返回object)
【构造实现】
function type(obj){
return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
}
【示例1】
console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function Person(){};
console.log(Object.prototype.toString.call(new Person));//[object Object]
【示例2】
function type(obj){
return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
}
console.log(type("jerry"));//"string"
console.log(type(12));//"number"
console.log(type(true));//"boolean"
console.log(type(undefined));//"undefined"
console.log(type(null));//"null"
console.log(type({name: "jerry"}));//"object"
console.log(type(function(){}));//"function"
console.log(type([]));//"array"
console.log(type(new Date));//"date"
console.log(type(/\d/));//"regexp"
function Person(){};
console.log(type(new Person));//"object"
constructor属性检测
3. constructor属性
【输出结果】返回function 数据类型(){[native code]}或function 自定义类型(){}的形式。
【功能特点】
[a] 能够识别基本数据类型、内置对象类型及自定义类型 [b] 无法识别undefined和null,调用时会报错
【构造实现】
function type(obj){
var temp = obj.constructor.toString();
return temp.replace(/^function (\w+)\(\).+$/,'$1');
}
【示例1】
console.log(("jerry").constructor);//function String(){[native code]}
console.log((12).constructor);//function Number(){[native code]}
console.log((true).constructor);//function Boolean(){[native code]}
//console.log((undefined).constructor);//报错
//console.log((null).constructor);//报错
console.log(({name: "jerry"}).constructor);//function Object(){[native code]}
console.log((function(){}).constructor);//function Function(){[native code]}
console.log(([]).constructor);//function Array(){[native code]}
console.log((new Date).constructor);//function Date(){[native code]}
console.log((/\d/).constructor);//function RegExp(){[native code]}
function Person(){};
console.log((new Person).constructor);//function Person(){}
【示例2】
function type(obj){
var temp = obj.constructor.toString().toLowerCase();
return temp.replace(/^function (\w+)\(\).+$/,'$1');
}
console.log(type("jerry"));//"string"
console.log(type(12));//"number"
console.log(type(true));//"boolean"
//console.log(type(undefined));//错误
//console.log(type(null));//错误
console.log(type({name: "jerry"}));//"object"
console.log(type(function(){}));//"function"
console.log(type([]));//"array"
console.log(type(new Date));//"date"
console.log(type(/\d/));//"regexp"
function Person(){};
console.log(type(new Person));//"person"
instanceof操作符检测
4. instanceof操作符
【输出结果】布尔值true或false。
【功能特点】
[a] 能够识别内置对象类型、自定义类型及其父类型 [b] 无法识别基本数据类型,返回false [c] 无法识别undefined和null,调用时会报错
【代码示例】
console.log("jerry" instanceof String);//false
console.log(12 instanceof Number);//false
console.log(true instanceof Boolean);//false
//console.log(undefined instanceof Undefined);//报错
//console.log(null instanceof Null);//报错
console.log({name: "jerry"} instanceof Object);//true
console.log(function(){} instanceof Function);//true
console.log([] instanceof Array);//true
console.log(new Date instanceof Date);//true
console.log(/\d/ instanceof RegExp);//true
function Person(){};
console.log(new Person instanceof Person);//true
console.log(new Person instanceof Object);//true
以上即为JavaScript中4种类型识别方法的详细解析,希望这些内容能帮助您更好地理解和使用类型检测技术。
JS动态构建复杂嵌套JSON对象的方法
2026-11-05 15:41:52
JS负数取余为什么是负数?详解余数与模运算区别
2026-11-05 13:34:41
JS 解压出现乱码或报错怎么解决?
2026-11-05 12:32:38
JS 中 startsWith、indexOf 与正则判断字符串前缀的对比
2026-11-04 22:19:43
JS解析Excel后怎么将数据转换为JSON格式
2026-11-04 20:58:28
