📌 前言:为什么空对象检测如此重要?
在开发中我们经常会遇到这样的场景:
js
if(isEmpty(userInfo)){
// 跳转登录页
}
🛠️ 四种主流检测方案对比
方案一:Object.keys() 基础版
js
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
✅ 适用场景:普通数据对象检测
❌ 致命缺陷:
- 漏检不可枚举属性
- 无视Symbol类型键值
- 对特殊对象处理不当
方案二:JSON.stringify() 取巧法
js
function isEmpty(obj) {
return JSON.stringify(obj) === '{}';
}
⚠️ 三大隐患:
- 过滤
undefined
和函数属性 - 无法处理循环引用
- Date/RegExp等对象误判
方案三:Reflect.ownKeys() 终极方案
js
function isStrictEmpty(obj) {
return Reflect.ownKeys(obj).length === 0;
}
💡 核心优势:
- 捕获所有类型键值(含Symbol)
- 检测不可枚举属性
- 精准识别特殊对象
方案四:for...in 循环法(不推荐)
js
function isEmptyLoop(obj) {
for(let key in obj){
if(obj.hasOwnProperty(key)) return false;
}
return true;
}
🚨 重大缺陷:
- 可能误判原型链属性
- 性能最差(平均慢3倍)
方法对比表格
方法 | Symbol 键 | 不可枚举属性 | 原型链属性 | 特殊对象处理 |
---|---|---|---|---|
Object.keys() |
❌ | ❌ | ❌ | ✅ |
JSON.stringify() |
❌ | ❌ | ❌ | ❌ |
Reflect.ownKeys() |
✅ | ✅ | ❌ | ✅ |
for...in + 检查 |
❌ | ❌ | ✅ | ✅ |
💻 核心API深度对比:Object.keys vs Reflect.ownKeys
js
const secretKey = Symbol('SECRET');
const obj = {
[secretKey]: '绝密数据',
2: '数字键',
'name': '张三'
};
Object.defineProperty(obj, 'hiddenProp', {
value: '隐藏属性',
enumerable: false
});
console.log('Object.keys:', Object.keys(obj));
// 输出: ['2', 'name']
console.log('Reflect.ownKeys:', Reflect.ownKeys(obj));
// 输出: ['2', 'name', 'hiddenProp', Symbol(SECRET)]
Object.keys 与 Reflect.ownKeys 的核心区别
示例说明
🚀 性能实测数据
通过Benchmark.js测试10万次操作:
🛑 常见误区警示
-
数组误判问题
jsisEmpty([]) // 返回true,但空数组≠空对象!
-
特殊对象陷阱
jsisEmpty(new Date()) // 多数方案返回true
-
循环引用崩溃
jsconst obj = { self: null }; obj.self = obj; JSON.stringify(obj); // 抛出异常
🔧 最佳实践推荐
js
// 通用场景检测
const safeIsEmpty = obj => {
return Object.prototype.toString.call(obj) === '[object Object]'
&& Object.keys(obj).length === 0;
}
// 严格模式检测
const strictIsEmpty = obj => {
return Reflect.ownKeys(obj).length === 0
&& Object.getPrototypeOf(obj) === Object.prototype;
}