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