function getType(value) {
let type = typeof value;
if (type !== 'object') { // 如果是基本数据类型,直接返回
return type;
}
// 如果是引用数据类型,再进一步判断,正则返回结果
return Object.prototype.toString.call(value).replace(/^\[object (\S+)\]$/, '$1');
}
getType(123); // number
getType('xxx'); // string
getType(() => {}); // function
getType([]); // Array
getType({}); // Object
getType(null); // Null
封装Object.prototype.toString.call()
最新推荐文章于 2024-07-08 13:12:38 发布