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

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

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 Relatório

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