/
6_10.js
54 lines (47 loc) · 1.19 KB
/
6_10.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
class Bicycle {
constructor(opts = {}) {
this._style = opts.style;
this._size = opts.size;
this._tape_color = opts.tape_color;
this._front_shock = opts.front_shock;
this._rear_shock = opts.rear_shock;
}
get style() { return this._style; }
get size() { return this._size; }
get tape_color() { return this._tape_color; }
get front_shock() { return this._front_shock; }
get rear_shock() { return this._rear_shock; }
// checking 'style' starts down a slippery slope
spares() {
if (this.style === 'road') {
return {
chain: '11-speed',
tire_size: '23', // millimeters
tape_color: this.tape_color,
};
} else {
return {
chain: '11-speed',
tire_size: '2.1', // inches
front_shock: this.front_shock,
};
}
}
// ...
}
let bike;
bike = new Bicycle({
style: 'mountain',
size: 'S',
front_shock: 'Manitou',
rear_shock: 'Fox',
});
console.log(bike.spares());
// => { chain: '11-speed', tire_size: '2.1', front_shock: 'Manitou' }
bike = new Bicycle({
style: 'road',
size: 'M',
tape_color: 'red',
});
console.log(bike.spares());
// => { chain: '11-speed', tire_size: '23', tape_color: 'red' }