|
6 | 6 | GameObject |
7 | 7 | createdAt |
8 | 8 | dimensions |
9 | | - destroy() // prototype method -> returns the string 'Game object was removed from the game.'*/ |
| 9 | + destroy() // prototype method -> returns the string 'Game object was removed from the game.' */ |
10 | 10 |
|
11 | | - function GameObject (gameobj) { |
12 | | - this.createdAt = gameobj.createdAt; |
13 | | - this.dimensions = gameobj.dimensions; |
14 | | - } |
| 11 | +function GameObject(gameobj) { |
| 12 | + this.createdAt = gameobj.createdAt; |
| 13 | + this.dimensions = gameobj.dimensions; |
| 14 | +} |
15 | 15 |
|
16 | | - GameObject.prototype.distroy = function(){ |
17 | | - console.log('Game object was removed from the game'); |
18 | | - } |
| 16 | +GameObject.prototype.distroy = () => { |
| 17 | + return 'Game object was removed from the game'; |
| 18 | +}; |
19 | 19 | /* |
20 | 20 | NPC |
21 | 21 | hp |
22 | 22 | name |
23 | 23 | takeDamage() // prototype method -> returns the string '<object name> took damage.' |
24 | 24 | // should inherit destroy() from GameObject's prototype |
25 | 25 | */ |
26 | | - function NPC(npcObj){ |
27 | | - GameObject.call(this,npcObj); |
28 | | - this. hp = npcObj.hp; |
29 | | - this.name = npcObj.name; |
30 | | - } |
| 26 | +function NPC(npcObj) { |
| 27 | + GameObject.call(this, npcObj); |
| 28 | + this.hp = npcObj.hp; |
| 29 | + this.name = npcObj.name; |
| 30 | +} |
31 | 31 |
|
32 | | - NPC.prototype.takeDamage = function(){ |
33 | | - console.log(`${this.name} took damage`); |
34 | | - } |
| 32 | +NPC.prototype.takeDamage = () => { |
| 33 | + return `${this.name} took damage`; |
| 34 | +}; |
35 | 35 | /* |
36 | 36 | Humanoid |
37 | 37 | faction |
|
41 | 41 | // should inherit destroy() from GameObject through NPC |
42 | 42 | // should inherit takeDamage() from NPC |
43 | 43 | */ |
44 | | - function Humanoid (humanoidObj){ |
45 | | - GameObject.call(this.humanoidObj); |
46 | | - this.faction = humanoidObj.faction; |
47 | | - this.weapons = humanoidObj.weapons; |
48 | | - this.language = humanoidObj.language |
49 | | - } |
| 44 | +function Humanoid(humanoidObj) { |
| 45 | + GameObject.call(this.humanoidObj); |
| 46 | + this.faction = humanoidObj.faction; |
| 47 | + this.weapons = humanoidObj.weapons; |
| 48 | + this.language = humanoidObj.language; |
| 49 | +} |
50 | 50 |
|
51 | | - Humanoid.prototype.greet = function(){ |
52 | | - console.log(`${this.name} offers greetings in ${this.language}`); |
53 | | - } |
| 51 | +Humanoid.prototype.greet = () => { |
| 52 | + return `${this.name} offers greetings in ${this.language}`; |
| 53 | +}; |
54 | 54 | /* |
55 | 55 | Inheritance chain: Humanoid -> NPC -> GameObject |
56 | 56 | Instances of Humanoid should have all of the same properties as NPC and GameObject. |
57 | 57 | Instances of NPC should have all of the same properties as GameObject. |
58 | 58 |
|
59 | 59 | Example: |
60 | 60 | */ |
61 | | - const hamsterHuey = new Humanoid({ |
62 | | - createdAt: new Date(), |
63 | | - dimensions: { |
64 | | - length: 2, |
65 | | - width: 1, |
66 | | - height: 1, |
67 | | - }, |
68 | | - hp: 5, |
69 | | - name: 'Hamster Huey', |
70 | | - faction: 'Gooey Kablooie', |
71 | | - weapons: [ |
72 | | - 'bubblegum', |
73 | | - ], |
74 | | - language: 'Hamsterish', |
75 | | - }); |
76 | | - |
77 | | - hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' |
78 | | - hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' |
79 | | - hamsterHuey.destroy(); // returns 'Game object was removed from the game.' |
80 | | - |
| 61 | +const hamsterHuey = new Humanoid({ |
| 62 | + createdAt: new Date(), |
| 63 | + dimensions: 2, |
| 64 | + hp: 5, |
| 65 | + name: 'Hamster Huey', |
| 66 | + faction: 'Gooey Kablooie', |
| 67 | + weapons: ['bubblegum'], |
| 68 | + language: 'Hamsterish', |
| 69 | +}); |
81 | 70 |
|
| 71 | +hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' |
| 72 | +hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' |
| 73 | +hamsterHuey.destroy(); // returns 'Game object was removed from the game.' |
82 | 74 |
|
83 | 75 | /* eslint-disable no-undef */ |
84 | 76 |
|
|
0 commit comments