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

213
Vistas
Defining multiple names for the same getter/setter function in a JavaScript class

How can I assign multiple names to the same getter/setter function inside a JS class? I know I can just do something like this:

class Example
{
    static #privateVar = 0;

    static get name(){ /* code and stuff */ return this.#privateVar; }
    static get anotherName(){ /* code and stuff */ return this.#privateVar; }

    static set name(value){ /* validating input values or something here */ this.#privateVar = value; }
    static set anotherName(value){ /* validating input values or something here */ this.#privateVar = value; }
}

but is there a simple way to give the same function multiple names without copying the code? I know I don't need different functions, but if someone else is using the class (or I just forget) and wants to use a different name for the function (i.e, different abbreviations, grey/gray, etc.), it would be convenient.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can simply return the value from the other function:

static get anotherName() {
  return this.name;
}

and

static set anotherName(value) {
  this.name = value;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use Object.getOwnPropertyDescriptor and Object.defineProperty to copy the accessors:

class Example {
    static #privateVar = 0;

    static get name(){ /* code and stuff */ return this.#privateVar; }
    static set name(value){ /* validating input values or something here */ this.#privateVar = value; }

    static {
        Object.defineProperty(this, 'anotherName', Object.getOwnPropertyDescriptor(this, 'name'));
    }
}
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