Advertisement
dimitrix85

04.Elemelons

Nov 10th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveClass() {
  2.     class Melon {
  3.         constructor(weight, melonSort) {
  4.             if (new.target === Melon) {
  5.                 throw new Error("Canon instantiate directly.");
  6.             }
  7.             this.weight = weight;
  8.             this.melonSort = melonSort;
  9.             this.element = "";
  10.             this._elementIndex = this.weight * this.melonSort.length;
  11.         }
  12.  
  13.         get elementIndex() {
  14.             return this._elementIndex;
  15.         }
  16.         toString() {
  17.             return `Element: ${this.element}\nSort: ${this.melonSort}\nElement Index: ${this.elementIndex}`
  18.         }
  19.     }
  20.  
  21.     class Watermelon extends Melon {
  22.         constructor(weight, melonSort) {
  23.             super(weight, melonSort)
  24.             this.element = "Water";
  25.         }
  26.  
  27.     }
  28.  
  29.     class Firemelon extends Melon {
  30.         constructor(weight, melonSort) {
  31.             super(weight, melonSort)
  32.             this.element = "Fire";
  33.         }
  34.     }
  35.  
  36.     class Earthmelon extends Melon {
  37.         constructor(weight, melonSort) {
  38.             super(weight, melonSort)
  39.             this.element = "Earth";
  40.         }
  41.     }
  42.  
  43.     class Airmelon extends Melon {
  44.         constructor(weight, melonSort) {
  45.             super(weight, melonSort)
  46.             this.element = "Air";
  47.         }
  48.     }
  49.  
  50.     class Melolemonmelon extends Airmelon {
  51.         constructor(weight, melonSort) {
  52.             super(weight, melonSort)
  53.             this.element = 'Water';
  54.             this.elements = ['Fire', 'Earth', 'Air', 'Water'];
  55.             this.elInd = 0;
  56.  
  57.         }
  58.         morph() {
  59.             this.element = this.elements[this.elInd++ % 4];
  60.         }
  61.     }
  62.  
  63.     return {
  64.         Melon,
  65.         Earthmelon,
  66.         Watermelon,
  67.         Firemelon,
  68.         Airmelon,
  69.         Melolemonmelon
  70.     }
  71. }
  72.  
  73.  
  74. function solveProto() {
  75.     function Melon(weight, melonSort) {
  76.         if (new.target === Melon) {
  77.             throw new Error("Abstract class cannot be instantiated directly");
  78.         }
  79.         this.weight = weight;
  80.         this.melonSort = melonSort;
  81.         this.element = "";
  82.         this._elementIndex = this.weight * this.melonSort.length;
  83.  
  84.         Object.defineProperty(this, "elementIndex", {
  85.             get: function () { return this._elementIndex }
  86.         });
  87.     }
  88.  
  89.  
  90.     function Watermelon(weight, melonSort) {
  91.         Melon.call(this,weight, melonSort);
  92.         this.element = "Water";
  93.     }
  94.  
  95.     Watermelon.prototype.toString = function() {
  96.         return `Element: ${this.element}\nSort: ${this.melonSort}\nElement Index: ${this.elementIndex}`
  97.     };
  98.  
  99.     // Watermelon.prototype = Object.create(Melon.prototype);
  100.     Object.setPrototypeOf(Watermelon,Melon);
  101.     Watermelon.prototype.constructor = Watermelon;
  102.    
  103.     function Firemelon(weight, melonSort) {
  104.         Melon.call(this,weight, melonSort);
  105.         this.element = "Fire";
  106.     }
  107.  
  108.     Firemelon.prototype.toString = function() {
  109.         return `Element: ${this.element}\nSort: ${this.melonSort}\nElement Index: ${this.elementIndex}`
  110.     };
  111.  
  112.     // Firemelon.prototype = Object.create(Melon.prototype);
  113.     Object.setPrototypeOf(Firemelon,Melon);
  114.     Firemelon.prototype.constructor = Firemelon;
  115.  
  116.     function Earthmelon(weight, melonSort) {
  117.         Melon.call(this,weight, melonSort);
  118.         this.element = "Earth";
  119.     }
  120.  
  121.     Earthmelon.prototype.toString = function() {
  122.         return `Element: ${this.element}\nSort: ${this.melonSort}\nElement Index: ${this.elementIndex}`
  123.     };
  124.  
  125.     // Earthmelon.prototype = Object.create(Melon.prototype);
  126.     Object.setPrototypeOf(Earthmelon,Melon);
  127.     Earthmelon.prototype.constructor = Earthmelon;
  128.  
  129.     function Airmelon(weight, melonSort) {
  130.         Melon.call(this,weight, melonSort);
  131.         this.element = "Air";
  132.     }
  133.  
  134.     Airmelon.prototype.toString = function() {
  135.         return `Element: ${this.element}\nSort: ${this.melonSort}\nElement Index: ${this.elementIndex}`
  136.     };
  137.  
  138.     //Airmelon.prototype = Object.create(Melon.prototype);
  139.     Object.setPrototypeOf(Airmelon,Melon);
  140.     Airmelon.prototype.constructor = Airmelon;
  141.  
  142.     function Melolemonmelon(weight, melonSort) {
  143.         Airmelon.call(this,weight, melonSort);
  144.         this.element = 'Water';
  145.         this.elements = ['Fire', 'Earth', 'Air', 'Water'];
  146.         this.elInd = 0;
  147.  
  148.     }
  149.  
  150.     Melolemonmelon.prototype.morph = function () {
  151.         this.element = this.elements[this.elInd++ % 4];
  152.     }
  153.  
  154.     Melolemonmelon.prototype.toString = function() {
  155.         return `Element: ${this.element}\nSort: ${this.melonSort}\nElement Index: ${this.elementIndex}`
  156.     };
  157.  
  158.     //Melolemonmelon.prototype = Object.create(Melon.prototype);
  159.     Object.setPrototypeOf(Melolemonmelon,Airmelon);
  160.     Melolemonmelon.prototype.constructor = Melolemonmelon;
  161.  
  162.     return {
  163.         Melon,
  164.         Earthmelon,
  165.         Watermelon,
  166.         Firemelon,
  167.         Airmelon,
  168.         Melolemonmelon
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement