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

161
Vistas
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 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