1class Rectangle {
2 constructor(height, width) {
3 this.height = height;
4 this.width = width;
5 }
6 // Getter
7 get area() {
8 return this.calcArea();
9 }
10 // Method
11 calcArea() {
12 return this.height * this.width;
13 }
14}
15
16const square = new Rectangle(10, 10);
17
18console.log(square.area); // 100
1class MyClass {
2 constructor() {
3 this.answer = 42;
4 }
5}
6
7const obj = new MyClass();
8obj.answer; // 42
1class Polygon {
2 constructor(...sides) {
3 this.sides = sides;
4 }
5 // Method
6 *getSides() {
7 for(const side of this.sides){
8 yield side;
9 }
10 }
11}
12
13const pentagon = new Polygon(1,2,3,4,5);
14
15console.log([...pentagon.getSides()]); // [1,2,3,4,5]