Skip to content
快看这页儿写了啥...

JS 实现类型判断方法

题干

  • 类型判断

题解

js
function DataType(tgt, type) {
    const dataType = Object.prototype.toString.call(tgt).replace(/\[object (\w+)\]/, "$1").toLowerCase();
    return type ? dataType === type : dataType;
}

DataType("isboyjc");      // "string"
DataType(212121);     // "number"
DataType(true);         // "boolean"
DataType([], "array");  // true
DataType({}, "array");  // false

相关

JS 数据类型判断的方式有哪些?

JS 判断数组类型的方式有哪些

贡献者

isboyjc's avatar isboyjc

浏览量(PV)  次  ·  独立访客(UV)  人次
不正经的前端 | 八股 · 欢迎 star ⭐