-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patha-class.js
More file actions
38 lines (30 loc) · 619 Bytes
/
a-class.js
File metadata and controls
38 lines (30 loc) · 619 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
class Adder {
#value = 0;
constructor(initial) {
this.#value = initial;
}
add(delta) {
this.#value += delta;
}
get() {
return this.#value;
}
map(fn) {
return new Adder(fn(this.#value));
}
set(x) {
this.#value = x;
}
}
// Usage
const counter = new Adder(10);
console.log({ step1: counter.get() });
counter.add(5);
console.log({ step2: counter.get() });
const twice = (a) => a * 2;
const counter2 = counter.map(twice);
console.log({ counter2: counter2.get() });
console.log({ step3: counter.get() });
counter.set(50);
console.log({ step4: counter.get() });