前端JS必用工具【js-tool-big-box】学习,获取数据的详细类型

之前我们习惯性的用typeof方法去判断数据类型,但慢慢的发现,typeof这个方法能力有限,基础的数据类型倒是还能判断,但是复杂一点,或者是null之类的假类型,就判断不出来了。

比如以下这些判断:

const numValue = 42;
    console.log(typeof(numValue)); // number
    const strValue = 'hello';
    console.log(typeof(strValue)); // string
    const booleanValue = true;
    console.log(typeof(booleanValue)); // boolean
    const undefinedValue = undefined;
    console.log(typeof(undefinedValue)); // undefined
    const nullValue = null;
    console.log(typeof(nullValue)); // object
    const objValue = {};
    console.log(typeof(objValue)); // object
    const arrayValue = [];
    console.log(typeof(arrayValue)); // object
    const functionValue = function(){};
    console.log(typeof(functionValue)); // function
    const dateValue = new Date();
    console.log(typeof(dateValue)); // object
    const regExpValue = /regex/;
    console.log(typeof(regExpValue)); // object

可以看出,其实用typeof来判断类型,经常有一种不靠谱的感觉。现在 js-tool-big-box中添加了新的工具方法,可以判断这些数据类型,而且使用便捷。

目录

1 项目中安装导入

2  方法使用

2.1 数据判断

2.2 判断效果展示

2.3 使用方法总结


1 项目中安装导入

 执行npm命令进行安装

npm i js-tool-big-box

导入dataBox对象,判断数据的详细类型添加到了这个对象下面

import { dataBox } from 'js-tool-big-box';

2  方法使用

2.1 数据判断
const numValue = 42;
    console.log('42的具体数据类型:', dataBox.getDataType(numValue));
    const strValue = 'hello';
    console.log('hello的具体数据类型:', dataBox.getDataType(strValue));
    const booleanValue = true;
    console.log('true的具体数据类型:', dataBox.getDataType(booleanValue));
    const undefinedValue = undefined;
    console.log('undefined的具体数据类型:', dataBox.getDataType(undefinedValue));
    const nullValue = null;
    console.log('null的具体数据类型:', dataBox.getDataType(nullValue));
    const objValue = {};
    console.log('{}的具体数据类型:', dataBox.getDataType(objValue));
    const arrayValue = [];
    console.log('[]的具体数据类型:', dataBox.getDataType(arrayValue));
    const functionValue = function(){};
    console.log('function的具体数据类型:', dataBox.getDataType(functionValue));
    const dateValue = new Date();
    console.log('date的具体数据类型:', dataBox.getDataType(dateValue));
    const regExpValue = /regex/;
    console.log('regex的具体数据类型:', dataBox.getDataType(regExpValue));
2.2 判断效果展示

 

2.3 使用方法总结
方法名返回值入参

getDataType

返回具体数据类型,是个字符串值,[object 类型]第一个参数必填,表示需要被判断的数据

 

  • 16
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

经海路大白狗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值