Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda