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

183
Visualizações
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 Respostas
Responde à pergunta

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