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

180
Vistas
How to call the child constructor function method?

When I try to call the method( getdetails() method of Teacher ) of child constructor function the parent constructor method is being called . Isn't the child method supposed to shadow the parent method . How to call the child constructor method getDetails()?

let Person = function() { };
Person.prototype.personName = "Smith";
Person.prototype.age = 37;
Person.prototype.getDetails = function() {
  return `Person Name: ${this.personName}. Age is ${this.age}`;
};

let Teacher = function() { };
Teacher.prototype.mainSubject = "Physics";
Teacher.prototype.getDetails = function() {
  return `Main subject is ${this.mainSubject}`;
};
Teacher.prototype = Object.create(Person.prototype); // inheritance

let teacher1 = new Teacher();

console.log(teacher1.getDetails());

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

0

You assign a new object to Teacher.prototype therefore losing everything you assigned to it beforehand. You should create the object before everything else.

let Person = function() { };
Person.prototype.personName = "Smith";
Person.prototype.age = 37;
Person.prototype.getDetails = function() {
  return `Person Name: ${this.personName}. Age is ${this.age}`;
};

let Teacher = function() { };
Teacher.prototype = Object.create(Person.prototype); // inheritance
Teacher.prototype.mainSubject = "Physics";
Teacher.prototype.getDetails = function() {
  return `Main subject is ${this.mainSubject}`;
};

let teacher1 = new Teacher();

console.log(teacher1.getDetails());

But nowadays you could also use classes instead of functions.

class Person {
  personName = "Smith";
  age = 37;
  
  getDetails() {
    return `Person Name: ${this.personName}. Age is ${this.age}`;
  }
}

class Teacher extends Person {
  mainSubject = "Physics";

  getDetails() {
    return `Main subject is ${this.mainSubject}`;
  }
}

let teacher1 = new Teacher();

console.log(teacher1.getDetails());

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