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

311
Visualizações
This is confusing, is there any tips so I can understand this better? (OOP Inheritances)

I am having difficulties understanding what to do here. Please bear with me I'm new to JavaScript and this section is giving me a hard time understanding how I can achieve this.

I have a total of 3 classes:

// 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;
  }
}

Now there is a general Person class that contains the shared code. Student and Mentor inherit behaviour and state information from Person using the keyword extends. They also have their own code that reflects behaviour and information only pertaining to themselves.

Student and Mentor are subclasses of the Person class, since they are extensions of that class. Person is the superclass in this relationship.

I need to write out the three classes defined above into a new file. Add additional code that instantiates a Student and uses the enroll() method on it. Do the same for Mentor and its specific methods. Experiment with your code to further explore what is and is not possible here.

Now for the inheritance part:

Change the version of the Person class so that it contains another method. Can this method be called on in each of the two subclasses?

Change the constructor for Person by adding a new field to it (like email). How does this change the subclasses?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Some comments.

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

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