[Solved] Person and Teacher (Class Inheritance and Prototypes)

  

3
Topic starter

Write a JS class Person and a class Teacher which extends Person. A Person should have a name and an email. A Teacher should have a name, an email, and a subject.

Input

There will be no input.

Output

Your function should return an object containing the classes Person and Teacher.

Example:

function personAndTeacher() {
    //TODO
 
    return {
        Person,
        Teacher
    }
}
1 Answer
2

The solution:

function solve() {
 
    class Person {
        constructor(name, email) {
            this.name = name;
            this.email = email;
        }
    }
 
    class Teacher extends Person {
        constructor(teacherName, teacherEmail, subject) {
            super(teacherName, teacherEmail);
            this.subject = subject;
        }
    }
 
    return {
        Person,
        Teacher
    }
}
 
 
let teacher = new Teacher("Ivan", "ivan@ivan.bg", "history");
console.log(teacher);
Share: