Skip to content

Commit a6e8c59

Browse files
committed
stage 13
1 parent 6bbc890 commit a6e8c59

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

stages/.eslintrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# private fields are not supported yet without babel
2+
parser: babel-eslint

stages/classes.mjs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,91 @@
11
export class Shape {
2+
#x;
3+
#y;
4+
#col;
5+
26
constructor(x, y, col) {
3-
this._x = x;
4-
this._y = y;
5-
this._col = col;
7+
this.#x = x;
8+
this.#y = y;
9+
this.#col = col;
610
}
711

812
get x() {
9-
return this._x;
13+
return this.#x;
1014
}
1115

1216
set x(val) {
1317
if (typeof val !== 'number') {
1418
throw new TypeError('x must be a number');
1519
}
16-
this._x = val;
20+
this.#x = val;
21+
}
22+
23+
get y() {
24+
return this.#y;
25+
}
26+
27+
get col() {
28+
return this.#col;
1729
}
1830

1931
get area() {
2032
throw new Error('area is not a property of this shape');
2133
}
2234

2335
moveBy(x, y) {
24-
this._x += x;
25-
this._y += y;
36+
this.#x += x;
37+
this.#y += y;
2638
}
2739
}
2840

2941
export class Rectangle extends Shape {
42+
#width;
43+
#height;
44+
3045
constructor(x, y, width, height, col) {
3146
super(x, y, col);
32-
this._width = width;
33-
this._height = height;
47+
this.#width = width;
48+
this.#height = height;
49+
}
50+
51+
get width() {
52+
return this.#width;
53+
}
54+
55+
get height() {
56+
return this.#height;
3457
}
3558

3659
get area() {
37-
return this._width * this._height;
60+
return this.width * this.height;
3861
}
3962

4063
draw(ctx) {
41-
ctx.fillStyle = this._col;
42-
ctx.fillRect(this._x, this._y, this._width, this._height);
64+
ctx.fillStyle = this.col;
65+
ctx.fillRect(this.x, this.y, this.width, this.height);
4366
}
4467
}
4568

4669
export class Circle extends Shape {
70+
#r;
71+
4772
constructor(x, y, r, col) {
4873
super(x, y, col);
49-
this._r = r;
74+
this.#r = r;
75+
}
76+
77+
get r() {
78+
return this.#r;
5079
}
5180

5281
get area() {
53-
return Math.PI * this._r ** 2;
82+
return Math.PI * this.r ** 2;
5483
}
5584

5685
draw(ctx) {
57-
ctx.fillStyle = this._col;
86+
ctx.fillStyle = this.col;
5887
ctx.beginPath();
59-
ctx.arc(this._x, this._y, this._r, 0, 2 * Math.PI);
88+
ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
6089
ctx.fill();
6190
}
6291
}

stages/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22
/**
3-
* Getters can be used for computed properties.
4-
*
5-
* Here, shapes come with `.area`.
3+
* JavaScript gives us syntax for private fields;
4+
* the only changes in this stage are in `classes.mjs`.
65
*/
76

87
import { Circle, Rectangle } from './classes.mjs';

0 commit comments

Comments
 (0)