forked from bloominstituteoftechnology/JavaScript-II-Mini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors.js
More file actions
201 lines (166 loc) · 4.43 KB
/
constructors.js
File metadata and controls
201 lines (166 loc) · 4.43 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// to test these problems you can run 'node constructors.js' in your terminal
// problem #1
// add a method to Animal's prototype called 'grow'
// when 'grow' is invoked log '<name> grew larger!'
function Animal(options) {
this.species = options.species;
this.name = options.name;
}
// add 'grow' to Animal's prototype here
Animal.prototype.grow = function() {
console.log(`${this.name} grew larger`);
}
Animal.prototype.greeting = function() {
console.log(`${this.name} says ${this.speak}`);
}
function Dog(dogAttributes) {
Animal.call(this, dogAttributes);
this.speak = dogAttributes.speak;
this.waggyTail = dogAttributes.waggyTail;
}
Dog.prototype = Object.create(Animal.prototype);
const grizzly = new Dog({
species: 'Canis cuteus',
name: 'Grizzly Bear',
speak: 'woof',
waggyTail: true,
});
// problem #2
// setup Cat to inherit from Animal
// the Animal constructor needs to be invoked with the 'options' argument
// Cat should have its prototype inherit from Animal
// instances of Cat should also have access to the 'grow' method
function Cat(catAttributes) {
// invoke Animal here with .call
Animal.call(this, catAttributes);
this.speak = catAttributes.speak;
this.waggyTail = catAttributes.waggyTail;
this.stripedFur = catAttributes.stripedFur;
}
Cat.prototype = Object.create(Animal.prototype);
const garfield = new Cat({
species: 'feline',
name: 'Garfield',
speak: 'meow',
stripedFur: false,
});
// connect the prototypes here
// if everything is setup properly the code below will print 'Foofie grew larger!'
// uncomment the code below to test your solution
const foofie = new Cat({
name: 'foofie',
species: 'feline',
speak: 'meow',
});
foofie.grow();
foofie.greeting();
//CLASS example
class Fruit {
constructor(fruitAttributes) {
this.type = fruitAttributes.type;
this.name = fruitAttributes.name;
this.isRipe = fruitAttributes.isRipe;
this.calories = fruitAttributes.calories;
}
//method within class, outside of constructor
calculateCalories() {
console.log(`${this.name} has ${this.calories * 200} calories`);
}
}
//subclasses, that inherit from fruit
//inherits prototype methods through "extend"
class Banana extends Fruit{
constructor(bananaAttributes) {
//inherits the different attributes
super(bananaAttributes);
this.doMonkeysEat = bananaAttributes.doMonkeysEat;
}
ripen() {
if(this.isRipe === false) {
this.isRipe = true;
}
}
}
class BananaMaca extends Banana {
constructor(bMacaAttributes) {
super(bMacaAttributes);
this.tastesLikeApple = bMacaAttributes.tastesLikeApple;
this.size = bMacaAttributes.size;
}
}
class Kiwi extends Fruit {
constructor(kiwiAttributes) {
super(kiwiAttributes);
this.isFuzzy = kiwiAttributes.isFuzzy;
}
}
//instances of
const newBanana = new BananaMaca({
doMonkeysEat: true,
name: 'Banana',
isRipe: false,
calories: 3,
type: 'tree',
});
const newKiwi = new Kiwi({
isFuzzy: true,
name: 'Kiwi',
isRipe: false,
calories: 3,
type: 'tree',
});
myBanana.ripen();
console.log(myBanana);
//constructors (extra example)
//constructor (capitalized)
function Fruit(fruitAttributes) {
this.type = fruitAttributes.type;
this.name = fruitAttributes.name;
this.isRipe = fruitAttributes.isRipe;
this.calories = fruitAttributes.calories;
}
//method
Fruit.prototype.calculateCalories = function() {
console.log(`${this.name} has ${this.calories * 200} calories`);
}
//subclasses, that inherit from fruit
function Banana(bananaAttributes) {
//inherits the different attributes
Fruit.call(this, bananaAttributes);
this.doMonkeysEat = bananaAttributes.doMonkeysEat;
}
function BananaMaca(bMacaAttributes) {
Banana.call(this, bMacaAttributes);
this.tastesLikeApple = bMacaAttributes.tastesLikeApple;
this.size = bMacaAttributes.size;
}
function Kiwi(kiwiAttributes) {
Fruit.call(this, kiwiAttributes);
this.isFuzzy = kiwiAttributes.isFuzzy;
}
//inherits prototype (methods)
Banana.prototype = Object.create(Fruit.prototype);
Banana.prototype.ripen = function() {
if(this.isRipe === false) {
this.isRipe = true;
}
};
Kiwi.prototype = Object.create(Fruit.prototype);
//instances of
const newBanana = new Banana({
doMonkeysEat: true,
name: 'Banana',
isRipe: false,
calories: 3,
type: 'tree',
});
const newKiwi = new Kiwi({
isFuzzy: true,
name: 'Kiwi',
isRipe: false,
calories: 3,
type: 'tree',
});
// newKiwi.calculateCalories();
// newKiwi.ripen();
// newKiwi.isRipe;