2
头图

1. Judging by the stringify() method that comes with JSON

 function isEmptyObj(obj) {
    return JSON.stringify(obj) === '{}'
}
console.log('对象是否为空:', isEmptyObj({}))

2. for in loop judgment

 function isEmptyObj(obj) {
    for(let item in obj) {
        return true
    }
    return false
}    
console.log('对象是否为空:', isEmptyObj({}))

3. Use ES6's Object.keys() method

 function isEmptyObj(obj) {
    return Object.keys(obj).length === 0
}
console.log('对象是否为空:', isEmptyObj({}))

4.Object.getOwnPropertyNames() method

 function isEmptyObj(obj) {
    return Object.getOwnPropertyNames(obj).length === 0
} 
console.log('对象是否为空:', isEmptyObj({}))

前端小菜
266 声望8 粉丝

努力可以改变能力