以下方法都以objectA、objectB为例子
var objectA = {
name:"Jan",
age:18
}
var objectB = {}
1. JSON.stringify(object)
JSON.stringify(objectA) == "" //true;
JSON.stringify(objectB) == "" //false;
2. for in 循环判断
function isObjectEmpty(x){
for(let key in x){
return false;
}
return true;
}
console.log(isObjectEmpty(objectA)) //false
console.log(isObjectEmpty(objectB)) //true
3. ES6方法:Object.keys()
var arrA = Object.keys(objectA) //["name","age"]
var arrB = Object.keys(objectB) //[]
4. Object.getOwnPropertyNames()
类似方法3
var arrC = Object.getOwnPropertyNames(objectA) //["name","age"]
var arrD = Object.getOwnPropertyNames(objectB) //[]
5. jquery方法: isEmptyObject
$.isEmptyObject(objectA); //false
$.isEmptyObject(objectB); //true
网友评论