Advertisement
Guest User

ClassHierarchy2

a guest
Aug 27th, 2020
832
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.         scaleParam(param) {
  13.             switch (this.units) {
  14.                 case 'm':
  15.                     param /= 10;
  16.                     break;
  17.                 case 'cm':
  18.                     break;
  19.                 case 'mm':
  20.                     param *= 10;
  21.                     break;
  22.                 default:
  23.                     break;
  24.             }
  25.             return param;
  26.         }
  27.         toString() {
  28.             return `Figures units: ${this.units}`;
  29.         }
  30.     }
  31.     class Circle extends Figure {
  32.         constructor(radius, units) {
  33.             super(units);
  34.             this._radius = radius;
  35.         }
  36.         get area() {
  37.             return Math.PI * this.radius * this.radius;
  38.         }
  39.         get radius() {
  40.             return this.scaleParam(this._radius);
  41.         }
  42.         toString() {
  43.             return `${super.toString()} Area: ${this.area} - radius: ${this.radius}`;
  44.         }
  45.     }
  46.     class Rectangle extends Figure {
  47.         constructor(width, height, units) {
  48.             super(units);
  49.             this._width = width;
  50.             this._height = height;
  51.         }
  52.         get area() {
  53.             return this.width * this.height;
  54.         }
  55.         get width() {
  56.             return this.scaleParam(this._width);
  57.         }
  58.         get height() {
  59.             return this.scaleParam(this._height);
  60.         }
  61.         toString() {
  62.             return `${super.toString()} Area: ${this.area} - width: ${this.width}, height: ${this.height}`;
  63.         }
  64.     }
  65.     return { Figure, Circle, Rectangle };
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement