JavaScript 实现一个判断数据类型的函数

1. 判断数据类型的方法

1. typeof

使用简单,但功能不完整,不能判断所有的数据类型,如下表所示:

类型结果
Undefined"undefined"
Null"object"
Boolean"boolean"
Number"number"
BigInt"bigint"
String"string"
Symbol"symbol"
Function"function"
其他任何对象"object"

2. toString

使用Object.prototype.toString.call()可正确判断所有数据类型,返回形如"[object XXX]"的字符串。

3. instanceof

instanceof运算符用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上。并不能用来判断数据类型。

function Car(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);

console.log(auto instanceof Car); // true
console.log(auto instanceof Object); // true

2. 封装判断数据类型函数

我们可以通过typeof来查看数据的类型,但是在 JavaScript 中,null、对象{}、数组[]、正则表达式/123/、日期new Date()都被认为是object,如下代码所示:

console.log(typeof 123); // number
console.log(typeof 'string'); // string
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
//----------------------------------------
console.log(typeof null); // object
console.log(typeof {}); // object
console.log(typeof []); // object
console.log(typeof /123/); // object
console.log(typeof new Date()); // object

此时若我们希望输入一个数组[],直接得到array这样的返回值,使用typeof就无法实现了。

那么如何直接得到array呢?

我们先来看下面一段代码:

console.log(Object.prototype.toString.call(null)); // [object Null]
console.log(Object.prototype.toString.call({})); // [object Object]
console.log(Object.prototype.toString.call([])); // [object Array]
console.log(Object.prototype.toString.call(/123/)); // [object RegExp]
console.log(Object.prototype.toString.call(new Date())); // [object Date]

可以看到,通过Object.prototype.toString.call(),将需要判断的数据类型放入call()中就可以得到需要的字符,如NullArrayRegExp等。

// 返回的是一个字符串
console.log(typeof Object.prototype.toString.call([])); // string

比如Object.prototype.toString.call([])返回[object Array],它是一个字符串,那么将这个字符串中除了Array以外的字符全删掉即可得到Array

// 字符串中除了 Array 以外的字符全删掉即可得到 Array
console.log(Object.prototype.toString.call([]).replace('[object ', '').replace(']', '')); // Array

为了和typeof的输出形式一致,将其返回的结果转化为小写即可。

// 转化为小写
console.log(Object.prototype.toString.call([]).replace('[object ', '').replace(']', '').toLowerCase()); // array

最后,将上述过程封装成自己的函数getType()

const getType = obj => {
    return typeof obj === 'object'
        ? Object.prototype.toString.call(obj).replace('[object ', '').replace(']', '').toLowerCase()
        : typeof obj;
};

首先判断typeof返回值是否为'object',若是则需要进行处理;若不是则直接返回即可。

验证一下:

console.log(getType(123)); // number
console.log(getType('string')); // string
console.log(getType(true)); // boolean
console.log(getType(undefined)); // undefined
console.log(getType(null)); // null
console.log(getType({})); // object
console.log(getType([])); // array
console.log(getType(/123/)); // regexp
console.log(getType(new Date())); // date

完成!


📘📘欢迎在我的博客上访问:
https://lzxjack.top/

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火星飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值