-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.js
55 lines (45 loc) · 1.16 KB
/
class.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
class Desert {
constructor(calories = 100) {
this.calories = calories;
this.ingredients = [];
}
addIngredient(ingredient) {
this.ingredients.push(ingredient);
}
setIngredients(ingredients) {
this.ingredients = ingredients;
}
getIngredients() {
return this.ingredients;
}
}
const vanilla = new Desert(200);
vanilla.addIngredient("sugar");
vanilla.addIngredient("milk");
console.log("Ingredients: ", vanilla.getIngredients());
class IceCream extends Desert {
constructor(flavor, calories, ingredients = [], toppings = []) {
super(calories); // Set the parent class properties
super.setIngredients(ingredients);
this.flavor = flavor;
this.toppings = toppings;
}
getFlavor() {
return this.flavor;
}
}
const vanillaIce = new IceCream(
"vanilla",
275,
["sugar", "milk"],
["chocochips", "jellies"]
);
console.log("Vanilla Ice", vanillaIce.getIngredients(), vanillaIce.getFlavor());
function Coffee(withSugar = false) {
this.withSugar = withSugar;
}
Coffee.prototype.hasSugar = function () {
return this.withSugar;
};
const myCoffee = new Coffee(true);
console.log("Coffee has sugar? ", myCoffee.hasSugar());