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

102
Vistas
Js Inheritance and private members

The following code crashes due to obscure js implementation details.

class A {

  constructor(x) {
    this.X = x
  }

  get X()  { throw new Error('Child should know ...')}
  set X(x) { throw new Error('Child should know ...')}

}

class B 
  extends A {
  #X

  get X()  { return this.#X }
  set X(x) { this.#X = x }
}

const b = new B(3)
  set X(x) { this.#X = x }
                     ^

TypeError: Cannot write private member #X to an object whose class did not declare it
    at B.set X [as X] ([...]/Test.js:17:22)
    at new A ([...]/Test.js:4:12)
    at new B ([...]/Test.js:12:1)
    at Object.<anonymous> ([...]/Test.js:20:11)

Is there a way to keep X private in class B without breaking inheritance logic ?

I hope someone could help me. Regards

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

0

Class B extends A {
 constructor(x) {
   super(x);
   this.X = x;
 }

 get X()  { return this.X }
 set X(x) { this.X = x }
}
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