forked from aychtang/giraffeMaker
-
Notifications
You must be signed in to change notification settings - Fork 419
/
giraffePseudoClassical.js
45 lines (37 loc) · 1.02 KB
/
giraffePseudoClassical.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
//pseudo-classical inheritance example
var Giraffe = function(name, height) {
this.name = name;
this.height = height;
this.hunger = 10;
};
Giraffe.prototype.isTallEnough = function(treeHeight) {
return this.height > treeHeight;
};
Giraffe.prototype.isHungry = function() {
return this.hunger > 0;
};
Giraffe.prototype.say = function(option) {
var sentences = {
'greet': 'Hello, my name is ' + this.name + ', it is nice to meet you.',
'notHungry': this.name + ' is not hungry.',
'notTallEnough': this.name + ' is too short to reach the trees.',
'ate': 'That was delicious!'
};
return console.log(sentences[option]);
};
Giraffe.prototype.eat = function() {
if (this.isHungry()) {
this.hunger -= this.height;
this.say('ate');
} else {
this.say('notHungry');
}
};
Giraffe.prototype.browse = function() {
if (this.isTallEnough(2)) {
this.eat();
} else {
this.say('notTallEnough')
}
};
var Stanley = new Giraffe('stanley', 5);