Advertisement
Guest User

ClassHierarchy

a guest
Aug 27th, 2020
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Hierarchy() {
  2.     class Figure {
  3.         constructor(units = 'cm') {
  4.             this.units = units;
  5.             if (new.target === Figure) {
  6.                 throw new TypeError('Figure class is abstract');
  7.             }
  8.         }
  9.         changeUnits(value) {
  10.             this.units = value;
  11.         }
  12.         toString() {
  13.             return `Figures units: ${this.units}`;
  14.         }
  15.     }
  16.     class Circle extends Figure {
  17.         constructor(radius, units) {
  18.             super(units);
  19.             this._radius = radius;
  20.         }
  21.         get area() {
  22.             return Math.PI * this.radius * this.radius;
  23.         }
  24.         get radius() {
  25.             let radius = this._radius;
  26.             switch (this.units) {
  27.                 case 'm':
  28.                     radius /= 10;
  29.                     break;
  30.                 case 'cm':
  31.                     break;
  32.                 case 'mm':
  33.                     radius *= 10;
  34.                     break;
  35.                 default:
  36.                     break;
  37.             }
  38.             return radius;
  39.         }
  40.         toString() {
  41.             return `${super.toString()} Area: ${this.area} - radius: ${this.radius}`;
  42.         }
  43.     }
  44.     class Rectangle extends Figure {
  45.         constructor(width, height, units) {
  46.             super(units);
  47.             this._width = width;
  48.             this._height = height;
  49.         }
  50.         get area() {
  51.             return this.width * this.height;
  52.         }
  53.         get width() {
  54.             let width = this._width;
  55.             switch (this.units) {
  56.                 case 'm':
  57.                     width /= 10;
  58.                     break;
  59.                 case 'cm':
  60.                     break;
  61.                 case 'mm':
  62.                     width *= 10;
  63.                     break;
  64.                 default:
  65.                     break;
  66.             }
  67.             return width;
  68.         }
  69.         get height() {
  70.             let height = this._height;
  71.             switch (this.units) {
  72.                 case 'm':
  73.                     height /= 10;
  74.                     break;
  75.                 case 'cm':
  76.                     break;
  77.                 case 'mm':
  78.                     height *= 10;
  79.                     break;
  80.                 default:
  81.                     break;
  82.             }
  83.             return height;
  84.         }
  85.         toString() {
  86.             return `${super.toString()} Area: ${this.area} - width: ${this.width}, height: ${this.height}`;
  87.         }
  88.     }
  89.     return { Figure, Circle, Rectangle };
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement