Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
How to tell if a JavaScript value is an empty object?

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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to MDN:

When using get the property will be defined on the instance's prototype

In your Foo example:

class Foo {
  get foo() { return 'foo'; }
}

const value = new Foo();

console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(value)));
// check if 'empty' (has only "constructor")
console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(value)).length === 1);

If you want a function that covers all example cases, you can use this:

function isEmpty(obj) {
  const proto = Object.getPrototypeOf(obj);
  return (Object.keys(obj).length === 0) && (
    proto === null
    || proto === Object.prototype
    || (Object.getOwnPropertyNames(proto).length === 1)
  );
}

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

[value1, value2, value3, value4, value5, value6]
  .map((v, i) => `value${i + 1} is ${isEmpty(v) ? "empty" : "not empty"}`)
  .forEach((v) => console.log(v));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda