|
2 | 2 | // Today your goal is to refactor all of this code to use ES6 Classes. |
3 | 3 | // The console.log() statements should still return what is expected of them. |
4 | 4 |
|
5 | | -function GameObject(options) { |
6 | | - this.createdAt = options.createdAt; |
7 | | - this.dimensions = options.dimensions; |
8 | | -} |
9 | 5 |
|
10 | | -GameObject.prototype.destroy = function() { |
11 | | - return `Object was removed from the game.`; |
12 | | -}; |
13 | 6 |
|
14 | | -function CharacterStats(characterStatsOptions) { |
15 | | - GameObject.call(this, characterStatsOptions); |
16 | | - this.hp = characterStatsOptions.hp; |
17 | | - this.name = characterStatsOptions.name; |
| 7 | +class GameObject { |
| 8 | + constructor(options){ |
| 9 | + this.createdAt = options.createdAt; |
| 10 | + this.dimensions = options.dimensions; |
| 11 | + }// Methods go here: |
| 12 | + destroy() { |
| 13 | + return `${this.name} was removed from the game.`; |
| 14 | + } |
| 15 | +} // End GameObject |
| 16 | + |
| 17 | +// function GameObject(options) { |
| 18 | +// this.createdAt = options.createdAt; |
| 19 | +// this.dimensions = options.dimensions; |
| 20 | +// } |
| 21 | +// GameObject.prototype.destroy = function() { |
| 22 | +// return `Object was removed from the game.`; |
| 23 | +// }; |
| 24 | + |
| 25 | +class CharacterStats extends GameObject { |
| 26 | + constructor(characterStatsOptions){ |
| 27 | + super(characterStatsOptions); |
| 28 | + this.hp = characterStatsOptions.hp; |
| 29 | + this.name = characterStatsOptions.name; |
| 30 | + }// Methods go here: |
| 31 | + takeDamage(){ |
| 32 | + return `${this.name} took damage.`; |
18 | 33 | } |
| 34 | +}// End CharacterStats Class |
19 | 35 |
|
20 | | -CharacterStats.prototype = Object.create(GameObject.prototype); |
21 | 36 |
|
22 | | -CharacterStats.prototype.takeDamage = function() { |
23 | | - return `${this.name} took damage.`; |
24 | | -}; |
| 37 | +// function CharacterStats(characterStatsOptions) { |
| 38 | +// GameObject.call(this, characterStatsOptions); |
| 39 | +// this.hp = characterStatsOptions.hp; |
| 40 | +// this.name = characterStatsOptions.name; |
| 41 | +// } |
25 | 42 |
|
26 | | -function Humanoid(humanoidOptions) { |
27 | | - CharacterStats.call(this, humanoidOptions); |
28 | | - this.faction = humanoidOptions.faction; |
29 | | - this.weapons = humanoidOptions.weapons; |
30 | | - this.language = humanoidOptions.language; |
31 | | -} |
| 43 | +// CharacterStats.prototype = Object.create(GameObject.prototype); |
| 44 | + |
| 45 | +// CharacterStats.prototype.takeDamage = function() { |
| 46 | +// return `${this.name} took damage.`; |
| 47 | +// }; |
| 48 | + |
| 49 | +class Humanoid extends CharacterStats { |
| 50 | + constructor(humanoidOptions){ |
| 51 | + super(humanoidOptions); |
| 52 | + this.faction = humanoidOptions.faction; |
| 53 | + this.weapons = humanoidOptions.weapons; |
| 54 | + this.language = humanoidOptions.language; |
32 | 55 |
|
33 | | -Humanoid.prototype = Object.create(CharacterStats.prototype); |
| 56 | + }// Methods go here: |
| 57 | + greet(){ |
| 58 | + return `${this.name} offers a greeting in ${this.language}.`; |
| 59 | + } |
| 60 | +} // End Humanoid Class |
34 | 61 |
|
35 | | -Humanoid.prototype.greet = function() { |
36 | | - return `${this.name} offers a greeting in ${this.language}.`; |
37 | | -}; |
| 62 | +// function Humanoid(humanoidOptions) { |
| 63 | +// CharacterStats.call(this, humanoidOptions); |
| 64 | +// this.faction = humanoidOptions.faction; |
| 65 | +// this.weapons = humanoidOptions.weapons; |
| 66 | +// this.language = humanoidOptions.language; |
| 67 | +// } |
| 68 | +// Humanoid.prototype = Object.create(CharacterStats.prototype); |
| 69 | +// Humanoid.prototype.greet = function() { |
| 70 | +// return `${this.name} offers a greeting in ${this.language}.`; |
| 71 | +// }; |
38 | 72 |
|
39 | 73 | const mage = new Humanoid({ |
40 | 74 | createdAt: new Date(), |
|
0 commit comments