Advertisement
Todorov_Stanimir

04. Elemelons Exercise: Prototypes and Inheritance

Nov 8th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     class Melon {
  3.         elements = {
  4.             Watermelon: 'Water',
  5.             Firemelon: 'Fire',
  6.             Earthmelon: 'Earth',
  7.             Airmelon: 'Air'
  8.         }
  9.         curElement = '';
  10.         constructor(weight, melonSort) {
  11.             if (new.target === Melon) {
  12.                 throw new TypeError("Abstract class cannot be instantiated directly");
  13.             }
  14.             this.weight = weight;
  15.             this.melonSort = melonSort;
  16.         }
  17.  
  18.         elementIndex() { return this.weight * this.melonSort.length }
  19.         toString() { return `Element: ${this.elements[this.curElement]}\nSort: ${this.melonSort}\nElement Index: ${this.elementIndex()}` }
  20.     }
  21.  
  22.     class Watermelon extends Melon {
  23.         curElement = 'Watermelon';
  24.         constructor(weight, melonSort) {
  25.             super(weight, melonSort)
  26.         }
  27.  
  28.     }
  29.  
  30.     class Firemelon extends Melon {
  31.         curElement = 'Firemelon';
  32.         constructor(weight, melonSort) {
  33.             super(weight, melonSort)
  34.         }
  35.     }
  36.  
  37.     class Earthmelon extends Melon {
  38.         curElement = 'Earthmelon';
  39.         constructor(weight, melonSort) {
  40.             super(weight, melonSort)
  41.         }
  42.     }
  43.  
  44.     class Airmelon extends Melon {
  45.         curElement = 'Airmelon';
  46.         constructor(weight, melonSort) {
  47.             super(weight, melonSort)
  48.         }
  49.     }
  50.  
  51.     class Melolemonmelon extends Watermelon {
  52.         curElement = 'Watermelon';
  53.         classes = ['Firemelon', 'Earthmelon', 'Airmelon', 'Watermelon'];
  54.         constructor(weight, melonSort) {
  55.             super(weight, melonSort)
  56.         }
  57.  
  58.         morph() { this.curElement = this.classes.shift(); this.classes.push(this.curElement) }
  59.     }
  60.     return { Melon, Watermelon, Firemelon, Earthmelon, Airmelon, Melolemonmelon }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement