|
1 | | -// Here we have a functioning solutoin to your challenge from yesterday. |
2 | | -// Today your goal is to refactor all of this code to use ES6 Classes. |
3 | | -// The console.log() statements should still return what is expected of them. |
4 | | - |
5 | | -function GameObject(options) { |
| 1 | +class GameObject { |
| 2 | + constructor(options){ |
6 | 3 | this.createdAt = options.createdAt; |
7 | 4 | this.dimensions = options.dimensions; |
| 5 | + } |
| 6 | + destroy () { |
| 7 | + return `Object was removed from the game.`; |
| 8 | + } |
8 | 9 | } |
9 | 10 |
|
10 | | -GameObject.prototype.destroy = function() { |
11 | | - return `Object was removed from the game.`; |
12 | | -}; |
13 | | - |
14 | | -function CharacterStats(characterStatsOptions) { |
15 | | - GameObject.call(this, characterStatsOptions); |
16 | | - this.hp = characterStatsOptions.hp; |
17 | | - this.name = characterStatsOptions.name; |
| 11 | +class CharacterStats extends GameObject { |
| 12 | + constructor(characterStatsOptions) { |
| 13 | + super(characterStatsOptions); |
| 14 | + this.hp = characterStatsOptions.hp; |
| 15 | + this.name = characterStatsOptions.name; |
| 16 | + } |
| 17 | + takeDamage () { |
| 18 | + return `${this.name} took damage.`; |
| 19 | + }; |
18 | 20 | } |
19 | 21 |
|
20 | | -CharacterStats.prototype = Object.create(GameObject.prototype); |
21 | | - |
22 | | -CharacterStats.prototype.takeDamage = function() { |
23 | | - return `${this.name} took damage.`; |
24 | | -}; |
25 | | - |
26 | | -function Humanoid(humanoidOptions) { |
27 | | - CharacterStats.call(this, humanoidOptions); |
28 | | - this.faction = humanoidOptions.faction; |
29 | | - this.weapons = humanoidOptions.weapons; |
30 | | - this.language = humanoidOptions.language; |
| 22 | +class Humanoid extends CharacterStats { |
| 23 | + constructor(humanoidOptions) { |
| 24 | + super(humanoidOptions); |
| 25 | + this.faction = humanoidOptions.faction; |
| 26 | + this.weapons = humanoidOptions.weapons; |
| 27 | + this.language = humanoidOptions.language; |
| 28 | + } |
| 29 | + greet () { |
| 30 | + return `${this.name} offers a greeting in ${this.language}.`; |
| 31 | + }; |
31 | 32 | } |
32 | 33 |
|
33 | | -Humanoid.prototype = Object.create(CharacterStats.prototype); |
34 | | - |
35 | | -Humanoid.prototype.greet = function() { |
36 | | - return `${this.name} offers a greeting in ${this.language}.`; |
37 | | -}; |
38 | | - |
39 | 34 | const mage = new Humanoid({ |
40 | | - createdAt: new Date(), |
41 | | - dimensions: { |
42 | | - length: 2, |
43 | | - width: 1, |
44 | | - height: 1 |
45 | | - }, |
46 | | - hp: 5, |
47 | | - name: 'Bruce', |
48 | | - faction: 'Mage Guild', |
49 | | - weapons: ['Staff of Shamalama'], |
50 | | - language: 'Common Toungue' |
| 35 | +createdAt: new Date(), |
| 36 | +dimensions: { |
| 37 | + length: 2, |
| 38 | + width: 1, |
| 39 | + height: 1 |
| 40 | +}, |
| 41 | +hp: 5, |
| 42 | +name: 'Bruce', |
| 43 | +faction: 'Mage Guild', |
| 44 | +weapons: ['Staff of Shamalama'], |
| 45 | +language: 'Common Toungue' |
51 | 46 | }); |
52 | 47 |
|
53 | 48 | const swordsman = new Humanoid({ |
54 | | - createdAt: new Date(), |
55 | | - dimensions: { |
56 | | - length: 2, |
57 | | - width: 2, |
58 | | - height: 2 |
59 | | - }, |
60 | | - hp: 15, |
61 | | - name: 'Sir Mustachio', |
62 | | - faction: 'The Round Table', |
63 | | - weapons: ['Giant Sword', 'Shield'], |
64 | | - language: 'Common Toungue' |
| 49 | +createdAt: new Date(), |
| 50 | +dimensions: { |
| 51 | + length: 2, |
| 52 | + width: 2, |
| 53 | + height: 2 |
| 54 | +}, |
| 55 | +hp: 15, |
| 56 | +name: 'Sir Mustachio', |
| 57 | +faction: 'The Round Table', |
| 58 | +weapons: ['Giant Sword', 'Shield'], |
| 59 | +language: 'Common Toungue' |
65 | 60 | }); |
66 | 61 |
|
67 | 62 | const archer = new Humanoid({ |
68 | | - createdAt: new Date(), |
69 | | - dimensions: { |
70 | | - length: 1, |
71 | | - width: 2, |
72 | | - height: 4 |
73 | | - }, |
74 | | - hp: 10, |
75 | | - name: 'Lilith', |
76 | | - faction: 'Forest Kingdom', |
77 | | - weapons: ['Bow', 'Dagger'], |
78 | | - language: 'Elvish' |
| 63 | +createdAt: new Date(), |
| 64 | +dimensions: { |
| 65 | + length: 1, |
| 66 | + width: 2, |
| 67 | + height: 4 |
| 68 | +}, |
| 69 | +hp: 10, |
| 70 | +name: 'Lilith', |
| 71 | +faction: 'Forest Kingdom', |
| 72 | +weapons: ['Bow', 'Dagger'], |
| 73 | +language: 'Elvish' |
79 | 74 | }); |
80 | 75 |
|
81 | 76 | console.log(mage.createdAt); // Today's date |
|
0 commit comments