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

225
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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