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

79
Vistas
Setting field in superclass isn't propagated to subclass

In an effort to reduce boilerplate in my constructors, I have an abstract class implementation that tries to set fields on the implementing class. A simplified version is below. The problem is that in the superclass constructor, the fields appear to be set, but outside of that constructor, they are undefined.

Code:

abstract class BaseClass {
  constructor(args: any) {
    Object.keys(args).forEach((key) => (this[key] = args[key]));
    console.log("constructed", this);
  }
}

class ChildClass extends BaseClass {
  a: number;
  b: string;

  print() {
    console.log(this);
  }
}

const val = new ChildClass({ a: 1, b: "str" });
val.print();

The output of this code is:

constructed ChildClass { a: 1, b: 'str' }
ChildClass { a: undefined, b: undefined }

Is there any way to make it so that BaseClass's constructor sets values that are accessible from ChildClass?

about 4 years ago · Juan Pablo Isaza
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