|
6 | 6 | GameObject |
7 | 7 | createdAt |
8 | 8 | dimensions |
9 | | - destroy() // prototype method -> returns the string 'Game object was removed from the game.' |
10 | | -
|
11 | | - NPC |
| 9 | + destroy() // prototype method -> returns the string 'Game object was removed from the game.' */ |
| 10 | +function GameObject(stats) { |
| 11 | + this.createdAt = new Date(); |
| 12 | + this.dimensions = stats.dimensions; |
| 13 | +} |
| 14 | +GameObject.prototype.destroy = function () { |
| 15 | + return 'Game object was removed from the game.'; |
| 16 | +}; // in this notation format, the function must be created outside of the object |
| 17 | +// strings not incorporating methods from the Object must use '', not `` |
| 18 | + /* NPC |
12 | 19 | hp |
13 | 20 | name |
14 | 21 | takeDamage() // prototype method -> returns the string '<object name> took damage.' |
15 | | - // should inherit destroy() from GameObject's prototype |
16 | | -
|
17 | | - Humanoid |
| 22 | + // should inherit destroy() from GameObject's prototype */ |
| 23 | +function NPC(stats) { |
| 24 | + GameObject.call(this, stats); |
| 25 | + this.hp = stats.hp; |
| 26 | + this.name = stats.name; |
| 27 | +} |
| 28 | +NPC.prototype = Object.create(GameObject.prototype); |
| 29 | +// ^^ you must complete the prototype chain here in order for it to work see p2 of your notes, the |
| 30 | +// example item doesn't stop at the first page |
| 31 | +NPC.prototype.takeDamage = function () { |
| 32 | + return `${this.name} took damage.`; // this line can use `` because it's calling a method |
| 33 | +}; |
| 34 | +/* Humanoid |
18 | 35 | faction |
19 | 36 | weapons |
20 | 37 | language |
21 | 38 | greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.' |
22 | 39 | // should inherit destroy() from GameObject through NPC |
23 | | - // should inherit takeDamage() from NPC |
24 | | -
|
25 | | - Inheritance chain: Humanoid -> NPC -> GameObject |
| 40 | + // should inherit takeDamage() from NPC */ |
| 41 | +function Humanoid(stats) { |
| 42 | + NPC.call(this, stats); |
| 43 | + this.faction = stats.faction; |
| 44 | + this.weapons = stats.weapons; |
| 45 | + this.language = stats.language; |
| 46 | +} |
| 47 | +Humanoid.prototype = Object.create(NPC.prototype); // don't forget to finish the prototype chain... |
| 48 | +Humanoid.prototype.greet = function () { |
| 49 | + return `${this.name} offers a greeting in ${this.language}.`; |
| 50 | +}; |
| 51 | + /* Inheritance chain: Humanoid -> NPC -> GameObject |
26 | 52 | Instances of Humanoid should have all of the same properties as NPC and GameObject. |
27 | 53 | Instances of NPC should have all of the same properties as GameObject. |
28 | 54 |
|
|
0 commit comments