-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_2.js
45 lines (36 loc) · 873 Bytes
/
class_2.js
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
39
40
41
42
43
44
45
class Stream {
#value;
#nextValue;
static #count = 0;
constructor(value, nextValue) {
this.#value = value;
this.#nextValue = nextValue;
Stream.#count++;
}
get value() {
return this.#value;
}
get next() {
this.#value = this.#nextValue(this.#value);
return this.#value;
}
static get count() {
return Stream.#count;
}
}
class ConstantStream extends Stream {
constructor(value) {
super(value, value => value);
}
}
class NextIntegerStream extends Stream {
constructor() {
super(0, value => value + 1);
}
}
const constant = new ConstantStream(0);
const nextInteger = new NextIntegerStream();
for (let i = 0; i < 10; i++) {
console.log(`constant[${i}] = ${constant.next}`);
console.log(`nextInteger[${i}] = ${nextInteger.next}`);
}