在AJAX请求之后,有时我的应用程序可能返回一个空对象,例如:
var a = {};
如何检查情况呢?
ECMA 5+:
// because Object.keys(new Date()).length === 0; // we have to do some additional check Object.keys(obj).length === 0 && obj.constructor === Object
Note, though, that this creates an unnecessary array (the return value of keys).
Pre-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:
Hoek
Hoek.deepEqual({}, {}); // true
ExtJS
Ext.Object.isEmpty({}); // true
AngularJS (version 1)
angular.equals({}, {}); // true
Ramda
R.isEmpty({}); // true