diff --git a/assignments/prototype-refactor.js b/assignments/prototype-refactor.js index e55ae39c0..6444906cf 100644 --- a/assignments/prototype-refactor.js +++ b/assignments/prototype-refactor.js @@ -2,39 +2,45 @@ // Today your goal is to refactor all of this code to use ES6 Classes. // The console.log() statements should still return what is expected of them. -function GameObject(options) { - this.createdAt = options.createdAt; - this.dimensions = options.dimensions; +class GameObject { + constructor (options) { + this.createdAt = options.createdAt; + this.dimensions = options.dimensions; + } + + destroy() { + return `Object was removed from the game.`; + } } -GameObject.prototype.destroy = function() { - return `Object was removed from the game.`; -}; -function CharacterStats(characterStatsOptions) { - GameObject.call(this, characterStatsOptions); - this.hp = characterStatsOptions.hp; - this.name = characterStatsOptions.name; +class CharacterStats extends GameObject { + constructor (characterStatsOptions) { + super(characterStatsOptions) + this.hp = characterStatsOptions.hp; + this.name = characterStatsOptions.name; + } + + takeDamage() { + return `${this.name} took damage.`; + } } -CharacterStats.prototype = Object.create(GameObject.prototype); -CharacterStats.prototype.takeDamage = function() { - return `${this.name} took damage.`; -}; +class Humanoid extends CharacterStats { + constructor (humanoidOptions) { + super(humanoidOptions) + this.faction = humanoidOptions.faction; + this.weapons = humanoidOptions.weapons; + this.language = humanoidOptions.language; + } -function Humanoid(humanoidOptions) { - CharacterStats.call(this, humanoidOptions); - this.faction = humanoidOptions.faction; - this.weapons = humanoidOptions.weapons; - this.language = humanoidOptions.language; + greet() { + return `${this.name} offers a greeting in ${this.language}.`; + } } -Humanoid.prototype = Object.create(CharacterStats.prototype); -Humanoid.prototype.greet = function() { - return `${this.name} offers a greeting in ${this.language}.`; -}; const mage = new Humanoid({ createdAt: new Date(),