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

229
Views
¿Cómo puedo llamar al método del bisabuelo en Javascript?

Me gustaría llamar a un método del bisabuelo a la clase Atleta,

¿Cómo puedo hacer eso?

Intenté usar super.printSentence pero eso no funcionó,

¿Es esta la forma correcta de invocar el método super.printPositon() en la clase Atleta?

¿Alguna sugerencia sobre cómo invocar este método printSentence?

 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());

¿Por qué no estoy definido para el campo de nombre?

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

0

Solo a this.printSentence()

En herencia (prototipo o no) this tiene acceso a todos los métodos.

A menos que esté utilizando un método privado como este:

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

Si lo piensa, si una Person tiene un name , cualquier clase que haya heredado de Person también tendrá un name de miembro. Esto también se aplica a cualquier instancia de una clase que herede de Person . por ejemplo:

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

0

printSentence() no devuelve un valor, así que return this.printSentence() devolverá undefined . Y dado que eso es lo que devuelve printGreatGrandFatherMethod , entonces console.log(soccerPlayer.printGreatGrandFatherMethod()); registrará undefined .

Lo mismo ocurre con printName() que tampoco devuelve un valor y, por lo tanto console.log(super.printName()) registrará 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!