JS中判断数据类型的四种方法

512 阅读2分钟

判断数据类型一般有四种方法

  • typeof 运算符
  • instanceof 运算符
  • constructor 属性
  • 函数借用:Object.prototype.toString.call(target)

1 typeof 运算符

基本认知:

  • typeof运算符适合于判断基本数据类型,但无法判断null,此外还可以判定函数类型
  • typeof运算符通常会结合instanceof运算符来使用

语法:对于数组、对象、正则会粗略的返回object,此外对于null也会返回object,虽然null是基本数据类型。

typeof 2; // => 'number'
typeof true; // => 'boolean'
typeof 'str'; // => 'string'
typeof undefined; // => 'undefined'
typeof Symbol(); // => 'symbol'
typeof BigInt('999446646455454'); // => 'bigint'

// typeof 无法判断null的类型
typeof null; // => 'object' 历史遗留问题且官方明确不会修复

// typeof 可以判断函数类型
typeof function a() {} // => 'function'

typeof new Object(); // => 'object'
typeof []; // => 'object'
typeof /^666/; // => 'object'
typeof new Date(); // => 'object'
typeof new Error(); // => 'object'
typeof new Set([1, 2, 3]); // => 'object'
typeof new Map([['name', 'Jack'],['age', 18]]); // => 'object'

拓展:typeof 判断 null 为何为object?

不同的对象在底层都表示为二进制,在JavaScript中二进制前三位都为0的话会被判断为object类型,null 的二进制表示是全 0,自然前三位也是 0,所以执行 typeof 时会返回“object”,这是个历史遗留问题。

2 instanceof 运算符

基本认知:instanceof运算符适合于判断引用数据类型,对基本数据类型进行判断全返回false,无法判断,而对null、undefined不能使用instanceof运算符

2 instanceof Number; //=> false
true instanceof Boolean; //=> false
'str' instanceof String; //=> false
Symbol() instanceof Symbol; //=> false
BigInt('999446646455454') instanceof BigInt; //=> false

new Object() instanceof Object; //=> true
[] instanceof Array; //=> true
/^666/ instanceof RegExp; //=>  true
new Date() instanceof Date; //=> true
new Error() instanceof Error; //=> true
new Set([1, 2, 3]) instanceof Set; //=> true
new Map([['name', 'Jack'],['age', 18]]) instanceof Map; //=> true
const fn = function (){}
fn instanceof Function; //=> true

3 constructor 属性

基本认知:无法判断null、undefined,因为这两个没有constructor属性

(2).constructor === Number; //=> true
(true).constructor === Boolean; //=> true
('str').constructor === String; //=> true
(Symbol()).constructor === Symbol; //=> true
(BigInt('999446646455454')).constructor === BigInt; //=> true
(new Object).constructor === Object; //=> true
([]).constructor === Array; //=> true
(/^666/).constructor === RegExp; //=>  true
(new Date()).constructor === Date; //=> true
(new Error()).constructor === Error; //=> true
(new Set([1, 2, 3])).constructor === Set; //=> true
(new Map([['name', 'Jack'],['age', 18]])).constructor === Map; //=> true
const fn = function (){}
(fn).constructor === Function; //=> true

4 函数借用:Object.prototype.toString.call(target)

基本认知:对toString()方法进行函数借用可以对全部类型进行判断。

Object.prototype.toString.call(2); //=> '[object Number]'
Object.prototype.toString.call(true); //=> '[object Boolean]'
Object.prototype.toString.call('str'); //=> '[object String]'
Object.prototype.toString.call(Symbol()); //=> '[object Symbol]'
Object.prototype.toString.call(BigInt('999446646455454')); //=> '[object BigInt]'
Object.prototype.toString.call(null); //=> '[object Null]'
Object.prototype.toString.call(undefined); //=> '[object Undefined]'
Object.prototype.toString.call(new Object); //=> '[object Object]'
Object.prototype.toString.call([]); //=> '[object Array]'
Object.prototype.toString.call(/^666/); //=> '[object RegExp]'
Object.prototype.toString.call(new Date()); //=> '[object Date]'
Object.prototype.toString.call(new Error()); //=> '[object Error]'
Object.prototype.toString.call(new Set([1, 2, 3])); //=> '[object Set]'
Object.prototype.toString.call(new Map([['name', 'Jack'],['age', 18]])); //=> '[object Map]'
const fn = function (){}
Object.prototype.toString.call(fn); //=> '[object Function]'