I came across this issue while debugging an app, and the other answers do not solve this!!
With the following use case :
class Foo {
get foo() { return 'foo'; }
}
const value = new Foo();
The following popular solutions do not work :
!Object.keys(value).length; // true
!Object.entries(value).length; // true
!Object.getOwnPropertyNames(value).length; // true
Neither is this solution :
function isEmpty(obj) {
for (var prop in obj) {
return false;
}
return true;
}
isEmpty(value); // true
To be clear, Foo
has a getter, and should not be considered empty!
In other words, I expect these results from these use cases :
const value1 = new class {}; // value1 is empty
const value2 = new class { get foo() { return 'foo'; } } // value2 is not empty
const value3 = {}; // value3 is empty
const value4 = { foo:'foo' }; // value4 is not empty
const value5 = Object.create(null); // value5 is empty
const value6 = Object.create(null); value6.foo = 'foo'; // value6 is not empty
How can this be evaluated reliably?