方法 1JSON.stringify()
const obj = {}
const isEmptyObject = (obj) => {
return JSON.stringify(obj) === '{}'
}
isEmptyObject(obj)
方法 2for in
const obj = {}
const isEmptyObject = (obj) => {
if (Object.prototype.toString.call(obj) === '[object Object]') {
for (var key in obj) {
return false
}
return true
} else {
return false
}
}
isEmptyObject(obj)
方法 3Object.getOwnPropertyNames()
const obj = {}
const isEmptyObject = (obj) => {
if (Object.prototype.toString.call(obj) === '[object Object]') {
return !Object.getOwnPropertyNames(obj).length
} else {
return false
}
}
isEmptyObject(obj)
方法 4Object.keys()
const obj = {}
const isEmptyObject = (obj) => {
if (Object.prototype.toString.call(obj) === '[object Object]') {
return !Object.keys(obj).length
} else {
return false
}
}
isEmptyObject(obj)