Advertisement
imehesz

ES6 Classes, inheritance and fat arrows

Feb 24th, 2015
1,165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Person {
  2.   constructor (name) {
  3.     this._name = name;
  4.   }
  5.  
  6.   get name () {
  7.     return this._name;
  8.   }
  9.  
  10.   set name (name) {
  11.     this._name = name;
  12.   }
  13.  
  14.   is () {
  15.     return "is unemployed!";
  16.   }
  17. }
  18.  
  19. class Worker extends Person {
  20.   constructor (name, work) {
  21.     super(name);
  22.     this._work = work;
  23.   }
  24.  
  25.   get work () {
  26.     return this._work;
  27.   }
  28.  
  29.   set work (work) {
  30.     this._work = work;
  31.   }
  32.  
  33.   is () {
  34.     if (this._work) {
  35.       return "is a " + this.work;
  36.     } else {
  37.       return super.is();
  38.     }
  39.   }
  40. }
  41.  
  42. var people = [new Worker("Lajos"), new Worker("Jozsi", "car mechanic")];
  43.  
  44. people.forEach(dude => {
  45.   console.log( dude.name + " dude " + dude.is());
  46. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement