1 数据类型
// 原始数据类型:
Number、String、Boolean、Undefined、Null、Symbol、BigInt。
// 复杂数据类型:
Object、Array、Function。
2 数据类型判断方式
下面两种方式返回值一样: string number boolean undefined function symbol bigint array date regExp object null
2.1 方式一
// 方式一
function judgeType(obj) {
// null
if (obj === null) return "null";
// array date regExp object
if (typeof obj === "object") {
if (obj instanceof Array) {
return "array";
} else if (obj instanceof Date) {
return "date";
} else if (obj instanceof RegExp) {
return "regExp";
} else if (obj instanceof Object) {
return "object";
}
}
// string number boolean undefined function symbol bigint
return typeof obj;
}
2.2 方式二
// 方式二
function judgeType(obj) {
// tostring会返回对应不同的标签的构造函数
const toString = Object.prototype.toString;
if (obj instanceof Element) {
return "element";
}
return toString.call(obj).slice(8, -1).toLowerCase();
}