js实现万能判断数据类型函数

文章介绍了在JavaScript开发中如何判断不同类型的数据,包括typeof、Array.isArray()和instanceof的使用,以及如何封装一个万能的数据类型判断函数`getTypeOf`,并展示了在dataType.js文件中导出各种类型判断方法的示例。
摘要由CSDN通过智能技术生成

在实际开发中,经常需要判断数据类型,由于数据类型多种多样,使用的api也多种多样,比如

  • 判断基础类型可以使用typeof,但是 typeof null 会被判定为 object;
  • 判断数组类型可以使用Array.isArray()
  • instanceof一般用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上,而不是用于判断数据类型;

例如

class Animal {
  constructor(name) {
    this.name = name;
  }
}

class Person{
  constructor(name) {
    this.name = name;
  }
}

const dog = new Animal("狗");
const cat = new Animal("猫");

console.log(dog instanceof Animal);	 // true
console.log(cat instanceof Person);	 // false
console.log(cat instanceof Object);  // true
console.log([] instanceof Object);	 // true

这里封装一个万能判断数据类型函数,用于判断各种数据类型

const getTypeOf = (arg) => Object.prototype.toString.call(arg).replace(/\[object (\w+)\]/, "$1").toLowerCase();

测试各种数据类型

const testObj = {
  isString: "测试数据类型",
  isNumber: 12,
  isBoolean: false,
  isNull: null,
  isUndefined: undefined,
  isSymbol: Symbol(1),
  isObject: {},
  isFunction: function () {},
  isArray: [],
  isDate: new Date(),
  isSet: new Set(),
  isWeakSet: new WeakSet(),
  isMap: new Map(),
  isWeakMap: new WeakMap(),
  isPromise: new Promise((resolve, reject) => {}),
  isRegExp: new RegExp(),
  isError: new Error(),
  isReflect: Reflect,
  isWindow: window,
  isLocation: window.location,
  isNavigator: window.navigator,
  isScreen: window.screen,
  isHistory: window.history,
  isDocument: window.document,
  isDivDom: document.createElement("div"),
  isSpanDom: document.createElement("span"),
  isADom: document.createElement("a"),
  isPDom: document.createElement("p"),
  isTextDom: document.createTextNode("文本节点"),
  isIDom: document.createElement("i"),
  // 其他dom元素类型一般为 htmlelement
};

for (const key in testObj) {
  console.log(`${key} ----> ${isTypeOf(testObj[key])}`);
}

控制台打印日志如下

在这里插入图片描述

在开发过程中,可以创建dataType.js文件,用于判断数据类型

const getTypeOf = (arg) => Object.prototype.toString.call(arg).replace(/\[object (\w+)\]/, "$1").toLowerCase();

export const isString = (value) => getTypeOf(value) === "string";
export const isNumber = (value) => getTypeOf(value) === "number";
export const isBoolean = (value) => getTypeOf(value) === "boolean";
export const isNull = (value) => getTypeOf(value) === "null";
export const isUndefined = (value) => getTypeOf(value) === "undefined";
export const isSymbol = (value) => getTypeOf(value) === "symbol";
export const isObject = (value) => getTypeOf(value) === "object";
export const isFunction = (value) => getTypeOf(value) === "function";
export const isArray = (value) => getTypeOf(value) === "array";
export const isDate = (value) => getTypeOf(value) === "date";
export const isSet = (value) => getTypeOf(value) === "set";
export const isWeakSet = (value) => getTypeOf(value) === "weakset";
export const isMap = (value) => getTypeOf(value) === "map";
export const isWeakMap = (value) => getTypeOf(value) === "weakmap";
export const isPromise = (value) => getTypeOf(value) === "promise";
export const isRegExp = (value) => getTypeOf(value) === "regexp";
export const isError = (value) => getTypeOf(value) === "error";
export const isBigInt = (value) => getTypeOf(value) === "bigint";
export const isReflect = (value) => getTypeOf(value) === "reflect";
// 需要其他类型可以继续往下加
import { isPromise, isMap } from "@/common/dataType.js"
// 各种业务代码
// ......
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值