这个方法是我踩了很多坑之后找到的,对数组等类型的对象都很好使,果断收藏!
function isEmpty(obj) {
// 检验 undefined 和 null
if (!obj && obj !== 0 && obj !== '') {
return true;
}
if (Array.prototype.isPrototypeOf(obj) && obj.length === 0) {
return true;
}
if (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0) {
return true;
}
return false;
}