ECMA 5+:
obj // null 和 undefined 判定 后再做后续判定
&& Object.keys(obj).length === 0 && obj.constructor === Object
ECMA 5 之前:
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
return false;
}
}
return JSON.stringify(obj) === JSON.stringify({});
}
jQuery:
jQuery.isEmptyObject({}); // true
Lodash:
_.isEmpty({}); // true
Underscore:
_.isEmpty({}); // true