如何判断是不是一个空对象有三个方法
1. JSON.stringify()
2. for in 配合hasOwnProperty()
3. Object.keys()
JSON.stringify()
let obj={}
if(JSON.stringify(obj)==="{}"){
console.log("obj是空对象")
}
for in 配合hasOwnProperty()
let obj = {}
for(key in obj){
if(obj.hasOwnProperty(key)){
console.log("obj是空对象")
}
}
Object.keys()
let obj = {}
let arr = Object.keys(obj)
let arr2 = Object.values(obj)
console.log(arr,arr2);
if (arr.length == 0 && arr2.length == 0) {
console.log("obj是空对象")
}