|
1 |
| -// [TODO - FILL IN ALL steps] |
| 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) { |
| 6 | + this.createdAt = options.createdAt; |
| 7 | + this.dimensions = options.dimensions; |
| 8 | +} |
| 9 | + |
| 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; |
| 18 | +} |
| 19 | + |
| 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; |
| 31 | +} |
| 32 | + |
| 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 | +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' |
| 51 | +}); |
| 52 | + |
| 53 | +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' |
| 65 | +}); |
| 66 | + |
| 67 | +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' |
| 79 | +}); |
| 80 | + |
| 81 | +console.log(mage.createdAt); // Today's date |
| 82 | +console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } |
| 83 | +console.log(swordsman.hp); // 15 |
| 84 | +console.log(mage.name); // Bruce |
| 85 | +console.log(swordsman.faction); // The Round Table |
| 86 | +console.log(mage.weapons); // Staff of Shamalama |
| 87 | +console.log(archer.language); // Elvish |
| 88 | +console.log(archer.greet()); // Lilith offers a greeting in Elvish. |
| 89 | +console.log(mage.takeDamage()); // Bruce took damage. |
| 90 | +console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. |
0 commit comments