Advertisement
milovan7

Class Hierarchy / JS Advanced

May 18th, 2021
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1.  
  2. function hierarchy() {
  3. class Figure {
  4. constructor(units = 'cm') {
  5. this.units = units;
  6. }
  7. changeUnits(value) {
  8. this.units = value;
  9. }
  10. metricConversion(num) {
  11. if (this.units === 'm') return num /= 10;
  12. if (this.units === 'mm') return num *= 10;
  13. return num;
  14. }
  15. toString() {
  16. if (this.hasOwnProperty('radius')) {
  17. return `Figures units: ${this.units} Area: ${this.area} - radius: ${this.radius}`;
  18. }
  19. if (this.hasOwnProperty('width')) {
  20. return `Figures units: ${this.units} Area: ${this.area} - width: ${this.width}, height: ${this.height}`;
  21. }
  22. }
  23. }
  24. class Circle extends Figure {
  25. constructor(radius, units) {
  26. super(units);
  27. this._radius = radius;
  28. }
  29. get area() {
  30. this.radius = this.metricConversion(this._radius);
  31. return Math.PI * Math.pow(this.radius, 2);
  32. }
  33. }
  34. class Rectangle extends Figure {
  35. constructor(width, height, units) {
  36. super(units);
  37. this._width = width;
  38. this._height = height;
  39. }
  40. get area() {
  41. this.width = this.metricConversion(this._width);
  42. this.height = this.metricConversion(this._height);
  43. return this.width * this.height;
  44. }
  45. }
  46. return { Figure, Circle, Rectangle };
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement