准确判断一个变量的数据类型

  • 准确判断一个变量的数据类型 由于typeof无法判断引用数据类型 所以需要调用 Object.prototype.toString.call上的方法
/**
 * 获取变量准确类型的函数
 * @param { * } target "目标变量"
 * @returns { String }
 */

const getType = (target)=> {
  if(typeof target !== 'object') return typeof target;
  return Object.prototype.toString.call(target).replace(/^\[object (\S+)\]$/, '$1');
}
posted @ 2023-02-02 13:45  HuangBingQuan  阅读(34)  评论(0编辑  收藏  举报