Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
JS: using object from parent class

I am having two classes and want to have an Object with values that are available for both classes. This is my sample code:

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();

Why is handle2.getContent() not showing the Things-Array? And (more important) what would be right?

i think the super() overrides the this.things... Is that true? how could i prevent this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just because you ran doThings() in handle1, doesn't mean doThings() was run in handle2. For you to get the output you want, you need to run doThings() in 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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda