Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!