Advertisement
braveheart1989

createComputerHierarchy

Nov 3rd, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createComputerHierarchy() {
  2.     class BaseClass {
  3.         constructor(manufacturer) {
  4.             if (new.target === BaseClass) {
  5.                 throw new Error("cant do that");
  6.             }
  7.             this.manufacturer = manufacturer;
  8.         }
  9.     }
  10.  
  11.     class Keyboard extends BaseClass {
  12.         constructor(manufacturer, responseTime) {
  13.             super(manufacturer);
  14.             this.responseTime = responseTime;
  15.         }
  16.     }
  17.  
  18.     class Monitor extends BaseClass {
  19.         constructor(manufacturer, width, height) {
  20.             super(manufacturer);
  21.             this.width = width;
  22.             this.height = height;
  23.         }
  24.     }
  25.  
  26.     class Battery extends BaseClass {
  27.         constructor(manufacturer, expectedLife) {
  28.             super(manufacturer);
  29.             this.expectedLife = expectedLife;
  30.         }
  31.     }
  32.  
  33.     class Computer extends BaseClass {
  34.         constructor(manufacturer, processorSpeed, ram, hardDiskSpace) {
  35.             super(manufacturer);
  36.             if (new.target === BaseClass) {
  37.                 throw new Error("cant do that");
  38.             }
  39.             this.processorSpeed = processorSpeed;
  40.             this.ram = ram;
  41.             this.hardDiskSpace = hardDiskSpace;
  42.         }
  43.     }
  44.  
  45.     class Laptop extends Computer {
  46.         constructor(manufacturer, processorSpeed, ram, hardDiskSpace, weight,color,battery) {
  47.             super(manufacturer, processorSpeed, ram, hardDiskSpace);
  48.             this.weight = weight;
  49.             this.color  = color;
  50.             this._battery = battery;
  51.         }
  52.  
  53.         set battery(bat) {
  54.             if(!bat instanceof Battery) {
  55.                 throw new TypeError("error")
  56.             }
  57.             this._battery = bat;
  58.         }
  59.         get battery() {
  60.             return this._battery;
  61.         }
  62.     }
  63.  
  64.     class Desktop extends Computer {
  65.         constructor(manufacturer, processorSpeed, ram, hardDiskSpace,keyboard,monitor) {
  66.             super(manufacturer, processorSpeed, ram, hardDiskSpace);
  67.             this._keyboard = keyboard;
  68.             this._monitor = monitor;
  69.         }
  70.  
  71.         set keyboard(keyboard) {
  72.             if(!keyboard instanceof Keyboard) {
  73.                 throw new TypeError("error")
  74.             }
  75.             this._keyboard = keyboard;
  76.         }
  77.  
  78.         get keyboard() {
  79.             return this._keyboard;
  80.         }
  81.  
  82.         set monitor(monitor) {
  83.             if(!monitor instanceof Monitor) {
  84.                 throw new TypeError("error")
  85.             }
  86.             return this._monitor = monitor;
  87.         }
  88.  
  89.         get monitor() {
  90.             return this._monitor;
  91.         }
  92.     }
  93.  
  94.     return {
  95.         Battery,
  96.         Keyboard,
  97.         Monitor,
  98.         Computer,
  99.         Laptop,
  100.         Desktop
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement