ECMA 7+ :
(ECMA 7+ :)
// because Object.entries(new Date()).length === 0;
// we have to do some additional check
Object.entries(obj).length === 0 && obj.constructor === Object
ECMA 5+ :
(ECMA 5+ :)
// because Object.keys(new Date()).length === 0;
// we have to do some additional check
Object.keys(obj).length === 0 && obj.constructor === Object
Pre-ECMA 5:
(ECMA 5之前的版本:)
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
return false;
}
}
return JSON.stringify(obj) === JSON.stringify({});
}
jQuery :
(jQuery的 :)
jQuery.isEmptyObject({}); // true
lodash :
(lodash :)
_.isEmpty({}); // true
Underscore :
(下划线 :)
_.isEmpty({}); // true
Hoek
(ek)
Hoek.deepEqual({}, {}); // true
ExtJS
(ExtJS)
Ext.Object.isEmpty({}); // true
AngularJS (version 1)
(AngularJS(版本1))
angular.equals({}, {}); // true
Ramda
(拉姆达)
R.isEmpty({}); // true
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…