// 判断对象是否为空对象
// 1.for...in循环
const obj = {};
const objIsEmpty1 = (obj) => {
for (let key in obj) {
return false;
}
return true;
};
//2.json.parse
const objIsEmpty2 = (obj) => {
if (JSON.stringify(obj) === '{}') {
return true;
}
return false;
};
//3.Object.keys
const objIsEmpty3 = (obj) => {
const key = Object.keys(obj);
if (key.length === 0) {
return true;
}
return false;
};
//4.Object.getOwnPropertyNames
const objIsEmpty4 = (obj) => {
const key = Object.getOwnPropertyNames(
if (key.length === 0) {
return true;
}
return false;
};
console.log(objIsEmpty1(obj));
console.log(objIsEmpty2(obj));
console.log(objIsEmpty3(obj));
console.log(objIsEmpty4(obj));
|