-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanaging-instaces.js
33 lines (27 loc) · 926 Bytes
/
managing-instaces.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
class Names {
constructor(...names) {
this.names = names;
}
filter1(selector) {
return new Names(...this.names.filter(selector));
}
filter2(selector) {
const constructor = Reflect.getPrototypeOf(this).constructor;
return new constructor(...this.names.filter(selector));
}
filter3(selector) {
const constructor =
Reflect.getPrototypeOf(this).constructor[Symbol.species] ||
Reflect.getPrototypeOf(this).constructor;
return new constructor(...this.names.filter(selector));
}
}
class SpecializedNames extends Names {
static get [Symbol.species]() {
return Names;
}
}
const specializedNames = new SpecializedNames("Java", "C#", "JavaScript");
console.log(specializedNames.filter1((name) => name.startsWith("Java")));
console.log(specializedNames.filter2((name) => name.startsWith("Java")));
console.log(specializedNames.filter3((name) => name.startsWith("Java")));