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

231
Views
How can I call the method of the great grandfather in Javascript?

I would like to call a method from the great grandfather to the Athlete class,

How can I do that?

I tried using super.printSentence but that did not work,

Is this the correct way to invoke the method, super.printPositon() in the Athlete class?

Any suggestion on how to invoke this printSentence method?

class Person {
  constructor(name) {
    this.name = name;
  }

  printName() {
    console.log(this.name);
  }
}

class TeamMate extends Person {
  constructor(name) {
    super(name);
  }

  printSentence() {
    console.log(super.printName(), "is an excellent teammate!" );
  }

}


class SoccerPlayer extends TeamMate {
  constructor(name, teamMateName, position) {
    super(name, teamMateName);
     this.teamMateName = teamMateName;
    this.position = position;
  }

  printPositon() {
    console.log("Positon: ", position);
  }
}


class Athlete extends SoccerPlayer{
  constructor(name, teamMateName, position, sport) {
    super(name, teamMateName, position);
    this.sport = sport;
  }

  printSport() {
    console.log("Favorite sport: ", this.sport);
  }

  //If Athlete class extends from SoccerPlayer and SoccerPlayer extends from 
    // the TeamMate class, how can I invoke the printSentence method 
    // from the TeamMate class in this current Athlete class?

  printGreatGrandFatherMethod() {
    return this.printSentence()
  }
}

const soccerPlayer = new Athlete('PLAYER1', 'Frederick', 'Defender', 'Soccer');
console.log(soccerPlayer.printGreatGrandFatherMethod());

Why am I getting undefined for the name field?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Just to this.printSentence()

In inheritance (prototype or not) the this has access to all the methods.

Unless you are using private method like this:

class ClassWithPrivateMethod {
  #privateMethod() {
    return 'hello world';
  }
}

If you think about it, if a Person has a name any class that inherited from Person will also have a member name. This is also true to any instance of a class that inherits from Person. for example:

const soccerPlayer = new SoccerPlayer('PLAYER1', 'MATE NAME', '1');
console.log(soccerPlayer.name); // Prints `PLAYER1`
about 4 years ago · Juan Pablo Isaza Report

0

printSentence() doesn't return a value, so return this.printSentence() will return undefined. And since that's what printGreatGrandFatherMethod returns, therefore console.log(soccerPlayer.printGreatGrandFatherMethod()); will log undefined.

Same goes for printName() which doesn't return a value either and therefore console.log(super.printName()) will log undefined

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!