settingsAccountsettings
Menusettings

Q: Person and Teacher (Class Inheritance and Prototypes)

+3 votes

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

 

asked in JavaScript category by user mitko
edited by user golearnweb

1 Answer

+2 votes

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

 

answered by user ak47seo
...