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

129
Vistas
JS: usando el objeto de la clase principal

Tengo dos clases y quiero tener un Objeto con valores que estén disponibles para ambas clases. Este es mi código de muestra:

 class Class1 { constructor() { this.things = []; this.testValue = "one two three"; } doThings() { for(let i = 0; i < 10; i++) { this.things[i] = "test "+i; } console.log("Class 1 doThings(): ", this.things); } getContent() { console.log("getContent Class1: ", this.things); } } class Class2 extends Class1 { constructor(elmnt){ super(); } getContent() { console.log("Class 2 getContent(): ", this.things); } getValue() { console.log("Value: ", this.testValue) } } let handle1 = new Class1(); let handle2 = new Class2(); handle1.getContent(); // empty because this.things() did not run handle1.doThings(); // create things... handle1.getContent(); // this shows the things handle2.getContent(); // this is empty even is doThings() is called before to produce things... handle2.getValue();

¿Por qué handle2.getContent() no muestra Things-Array? Y (más importante) ¿qué sería lo correcto?

Creo que super() anula this.things... ¿Es eso cierto? ¿Cómo podría prevenir esto?

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

0

El hecho de que haya ejecutado doThings() en handle1 no significa que se doThings() en handle2 . Para obtener el resultado que desea, debe ejecutar doThings() en handle2

 class Class1 { constructor() { this.things = []; this.testValue = "one two three"; } doThings() { for(let i = 0; i < 10; i++) { this.things[i] = "test "+i; } console.log("Class 1 doThings(): ", this.things); } getContent() { console.log("getContent Class1: ", this.things); } } class Class2 extends Class1 { constructor(elmnt){ super(); } getContent() { console.log("Class 2 getContent(): ", this.things); } getValue() { console.log("Value: ", this.testValue) } } let handle1 = new Class1(); let handle2 = new Class2(); handle1.getContent(); // empty because this.things() did not run handle1.doThings(); // create things... handle1.getContent(); // this shows the things handle2.doThings() // This needs to run handle2.getContent(); // this is empty even is doThings() is called before to produce things... handle2.getValue();

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