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

139
Visualizações
Accessing Object within its property's getters

In this simplified code, I want to give someGetters a parameter and to return a specific text, but the getters can't seem to access the parent Object's "this", "this" is pointed to someGetters.

var constants = {
    FLAG2: "testx2",
    FLAG4: "testx4",
    FLAG6: "testx6",
    someGetters: {
        get [this.FLAG2]() { // tried "this" but it points to someGetters
            return "Test Test"
        },
        get [constants.FLAG4]() { // tried constants but it says it's not defined yet
            return "Test Test Test Test"
        },
        get ["testx6"]() { // but this works
            return "Test Test Test Test Test Test"
        }
    }
};

console.log(constants.someGetters[constants.FLAG2]) // expected result: Test Test

Note that constants is exported from another js file called constants.ts.

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

0

You can't refer to the object's properties until you've finished constructing it. So add someGetters after you've assigned constants, and then refer to constants rather than this.

var constants = {
  FLAG2: "testx2",
  FLAG4: "testx4",
  FLAG6: "testx6",
};

constants.someGetters = {
  get [constants.FLAG2]() {
    return "Test Test"
  },
  get [constants.FLAG4]() {
    return "Test Test Test Test"
  },
  get ["testx6"]() { // but this works
    return "Test Test Test Test Test Test"
  }
}

console.log(constants);

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