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

133
Visualizações
Get all getters results as class variables without specific invoking

Is there a way I can get all the getters results on the instance without specific invoking? I want to get all class getters as simple members on the class by looping on the class member.

I have a class like this:

export class Test {
  constructor() {}

  get foo() {
    return 1
  }

  get bar() {
    return 2
  }
}

The use is create a new instance: const test = new Test()

Is there a way I can get all the getters as simple class variable members and not as functions? so I can pass the object from server to client.

Thanks!

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

0

class Test {
  constructor() {}

  get foo() {
     return 1
  }

  get bar() {
    return 2
  }
}

const allGetterKeys = Object.entries(Object.getOwnPropertyDescriptors(Test.prototype)).filter(([key, descriptor]) => typeof descriptor.get === 'function').map(([key]) => key);

const test = new Test();
const out = {};

for(const key of allGetterKeys)
{
  out[key] = test[key];
}

console.log(out);

The first line comes from this answer (it gets the key names of all the getters on the class). Then the for loop uses all those keys to get the output from the getter and put them into a plain object out. That object is just key value pairs.

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