MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1class Person {
2 constructor(name) {
3 this._name = name;
4 }
5
6 get name() {
7 console.log("Getting name");
8 return this._name;
9 }
10
11 set name(value) {
12 console.log("Setting name to:", value);
13 this._name = value;
14 }
15
16 get greeting() {
17 return "Hello, " + this._name;
18 }
19}
20
21const person = new Person("Alice");
22console.log("1. Initial name:", person.name);
23console.log("2. Greeting:", person.greeting);
24
25person.name = "Bob";
26console.log("3. After setting name:", person.name);
27console.log("4. New greeting:", person.greeting);
28
29// Test with multiple getters
30class Rectangle {
31 constructor(width, height) {
32 this._width = width;
33 this._height = height;
34 }
35
36 get area() {
37 return this._width * this._height;
38 }
39
40 get perimeter() {
41 return 2 * (this._width + this._height);
42 }
43
44 set width(w) {
45 console.log("Setting width to:", w);
46 this._width = w;
47 }
48
49 get width() {
50 return this._width;
51 }
52}
53
54const rect = new Rectangle(5, 10);
55console.log("5. Rectangle area:", rect.area);
56console.log("6. Rectangle perimeter:", rect.perimeter);