Tengo dificultades para entender qué hacer aquí. Tenga paciencia conmigo, soy nuevo en JavaScript y esta sección me está dificultando entender cómo puedo lograr esto.
Tengo un total de 3 clases:
// This class represents all that is common between Student and Mentor class Person { // moved here b/c it was identical constructor(name, quirkyFact) { this.name = name; this.quirkyFact = quirkyFact; } // moved here b/c it was identical bio() { return `My name is ${this.name} and here's my quirky fact: ${this.quirkyFact}`; } } class Student extends Person { // stays in Student class since it's specific to students only enroll(cohort) { this.cohort = cohort; } } class Mentor extends Person { // specific to mentors goOnShift() { this.onShift = true; } // specific to mentors goOffShift() { this.onShift = false; } }Ahora hay una clase de Persona general que contiene el código compartido. Student y Mentor heredan el comportamiento y la información de estado de Person usando la palabra clave extends. También tienen su propio código que refleja el comportamiento y la información que solo les pertenece a ellos.
Student y Mentor son subclases de la clase Person, ya que son extensiones de esa clase. La persona es la superclase en esta relación.
Necesito escribir las tres clases definidas anteriormente en un nuevo archivo. Agregue código adicional que crea una instancia de un estudiante y usa el método enroll() en él. Haga lo mismo con Mentor y sus métodos específicos. Experimente con su código para explorar más a fondo qué es y qué no es posible aquí.
Ahora para la parte de la herencia:
Cambie la versión de la clase Person para que contenga otro método. ¿Se puede llamar a este método en cada una de las dos subclases?
Cambie el constructor de Persona agregándole un nuevo campo (como correo electrónico). ¿Cómo cambia esto las subclases?
Algunos comentarios.
class Person { constructor(name, quirkyFact) { this.name = name; this.quirkyFact = quirkyFact; // Create new property - returns random boolean this.isAGigaChad = !!Math.round(Math.random() * 1); } get bio() { return `My name is ${this.name} and here's my quirky fact: ${this.quirkyFact}`; } speakProphecy() { return `"PHP sucks" -${this.name}`; } } class Student extends Person { // Every single class should have a constructor constructor(studentId, name, quirkyFact) { // If a class is inheriting from another, ALWAYS include // a super call. This will call the constructor of the // parent class. super(name, quirkyFact); this.studentId = studentId; // Initialize property this.cohort = null; } enroll(cohort) { this.cohort = cohort; } } class Mentor extends Person { constructor(name, quirkyFact) { super(name, quirkyFact); // By default, have them start off as not // on shift. This value can never be "undefined" this.onShift = false; } goOnShift() { this.onShift = true; } goOffShift() { this.onShift = false; } } const johnStudent = new Student(123, 'John', 'Dislike PHP'); console.log(johnStudent.bio); // Student has access to new speakProphecy method console.log(johnStudent.speakProphecy()); // Student has isAGigaChad property console.log(`${johnStudent.name} ${johnStudent.isAGigaChad ? 'is' : 'is not'} a gigachad`);