js通用检测数据类型方法

function getDataType(obj) {
    let type = typeof obj
  // 先判断是否为基础数据类型,直接返回
if (type !== 'object') return type;
  // 对于typeof返回结果是'object'的,在进行如下的判断,正则返回结果
return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1'); }

 

posted @ 2023-12-07 14:48  追风不跟风  阅读(17)  评论(0)    收藏  举报