Advertisement
kstoyanov

02. Inheriting and Replacing ToString

Oct 15th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function personAndTeacher() {
  2.   class Person {
  3.     constructor(name, email) {
  4.       this.name = name;
  5.       this.email = email;
  6.     }
  7.  
  8.     toString() {
  9.       return `${this.constructor.name} (name: ${this.name}, email: ${this.email})`;
  10.     }
  11.   }
  12.  
  13.   class Teacher extends Person {
  14.     constructor(name, email, subject) {
  15.       super(name, email);
  16.       this.subject = subject;
  17.     }
  18.  
  19.     toString() {
  20.       return super.toString().slice(0, -1)  + `, subject: ${this.subject})`;
  21.     }
  22.   }
  23.  
  24.   class Student extends Person {
  25.     constructor(name, email, course) {
  26.       super(name, email);    
  27.       this.course = course;
  28.     }
  29.  
  30.     toString() {
  31.       return super.toString().slice(0, -1)  + `, course: ${this.course})`;
  32.     }
  33.   }
  34.  
  35.   return {
  36.  
  37.     Person,
  38.     Teacher,
  39.     Student,
  40.   };
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement