使用toString判断所有js数据类型
发布日期:2021-05-14 15:54:37 浏览次数:15 分类:精选文章

本文共 2669 字,大约阅读时间需要 8 分钟。

JS������������������typeof���toString������isset | ������������

������

JavaScript������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ `typeof` ������������������������������������ `null`��������������������������������������������������������������������������������������������������������������� `toString()` ������������������

������toString() Method Explained

`toString()` ��� `Object.prototype` ��������������� `Object` ��������������������������������������������������������������������������������� [[Class]] Attribute������������������ `[object Xxx]`��������� `Xxx` ���������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������

function typeOf(target) {
return Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
}

������������������������������������������������������������������������������ `typeOf` ���������`Object.prototype.toString.call(target)` ������������������������������������������������������������������`[object Array]` ���������������`[object Null]` ������ `null`���`[object Undefined]` ���������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������

������������������������

function isEmpty(target) {
if (arguments.length > 1) {
return [...arguments].every(v => isEmpty(v));
}
const type = typeOf(target);
switch (type) {
case 'array':
case 'string':
case 'object':
return Object.keys(target).length === 0;
default:
return target === null || target === undefined;
}
}

������������ `isEmpty` ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ `null`���`undefined`������������������������������������������������������������������������������������������������������������������


������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:webpack Proxy工作原理
下一篇:使用原生 JS 完成头部吸顶操作

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年05月01日 02时13分27秒