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

166
Visualizações
how to call the methods of a parent class in a child class object without overriding in child in java script

Let's say I have a parent and a child class as shown below.

class Person { 

  constructor(name) { 
    this.name = name
  }

  greeting() { 
    console.log(`Hi! I'm ${this.name}`); 
  };

}

class Teacher extends Person { 
  constructor(first, subject) { 
    super(first); 

    this.subject = subject; 
  }
}

now lets say I create a global object of the child class

globalThis.teachers = new Teacher("Roby", "Math") 

this global object can directly access the parent class property name. So, I can call,

globalThis.teachers.name \\Will contain Roby

But can I call the method of the parent class, greeting() like this directly without overriding it like the following way?

globalThis.teachers.greeting() \\Will print in console

Snippet:

class Person { 

  constructor(name) { 
    this.name = name
  }

  greeting() { 
    console.log(`Hi! I'm ${this.name}`); 
  };

}

class Teacher extends Person { 
  constructor(first, subject) { 
    super(first); 

    this.subject = subject; 
  }
}

globalThis.teachers = new Teacher("Roby", "Math") 
console.log(globalThis.teachers.name);
globalThis.teachers.greeting();

Is it like every parent class method that I want to access, I will have to override it in the child and from there I will have to call super.greeting().

about 4 years ago · Santiago Trujillo
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