/
6_37.js
78 lines (65 loc) · 1.63 KB
/
6_37.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Bicycle {
constructor(opts = {}) {
this._size = opts.size;
this._chain = opts.chain || this.default_chain();
this._tire_size = opts.tire_size || this.default_tire_size();
}
get size() { return this._size; }
get chain() { return this._chain; }
get tire_size() { return this._tire_size; }
default_chain() { // <- common default
return '11-speed';
}
// ...
default_tire_size() {
throw new Error(`Not implemented: ${this.constructor.name} should have implemented...`);
}
}
class RoadBike extends Bicycle {
constructor(opts = {}) {
super(opts);
this._tape_color = opts.tape_color;
}
get tape_color() { return this._tape_color; }
spares() {
return {
chain: '11-speed',
tire_size: '23',
tape_color: this.tape_color,
};
}
default_tire_size() { // <- subclass default
return '23';
}
// ...
}
class MountainBike extends Bicycle {
constructor(opts = {}) {
super(opts);
this._front_shock = opts.front_shock;
this._rear_shock = opts.rear_shock;
}
get front_shock() { return this._front_shock; }
get rear_shock() { return this._rear_shock; }
spares() {
return Object.assign({}, super.spares(), {
front_shock: this.front_shock,
});
}
default_tire_size() { // <- subclass default
return '2.1';
}
// ...
}
class RecumbentBike extends Bicycle {
default_chain() {
return '10-speed';
}
}
const bent = new RecumbentBike({
size: 'L',
});
// => Error: Not implemented: RecumbentBike should have implemented...
// at RecumbentBike.default_tire_size ...
// at new Bicycle ...
// at new RecumbentBike ...