-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTemplate.js
57 lines (50 loc) · 1.31 KB
/
Template.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
// ES6 way Abstract Class:
class Sandwich {
constructor() {
if (new.target === Sandwich) {
throw new TypeError('Sorry this class is abstract and cannot be instantiated.');
}
}
makeBread() {
console.log('Making bread.');
return this; // because we want to chain.
}
addSalad() {
console.log('Adding salad.');
return this; // because we want to chain.
}
addSauces() {
console.log('Adding sauces.');
return this; // because we want to chain.
}
make() {
return this
.makeBread()
.addSalad()
.addToppings() // you HAVE to implement this method in child classes.
.addSauces();
}
// ES6 way Abstract method: MUST beimplemented in child classes.
addToppings() {
throw new Error('This method is abstract.');
}
}
class TurkeySandwich extends Sandwich {
// This method MUST be implemented as parent class needs it:
addToppings() {
console.log('Adding turkey.');
return this; // because we want to chain.
}
}
class ChickenSandwich extends Sandwich {
// This method MUST be implemented as parent class needs it:
addToppings() {
console.log('Adding chicken.');
return this; // because we want to chain.
}
}
const turkeySandwich = new TurkeySandwich();
const chickenSandwich = new ChickenSandwich();
turkeySandwich.make();
console.log('========================');
chickenSandwich.make();