From 30d88a41a96c3131ba28e6b4138aa23fd6e04f8a Mon Sep 17 00:00:00 2001 From: SunJieMing Date: Wed, 4 Oct 2017 12:42:48 -0700 Subject: [PATCH] Added prototype solution --- solutions.js | 93 ------------------------------------------------ src/prototype.js | 34 +++++++++++++++++- 2 files changed, 33 insertions(+), 94 deletions(-) delete mode 100644 solutions.js diff --git a/solutions.js b/solutions.js deleted file mode 100644 index 7a5f23d..0000000 --- a/solutions.js +++ /dev/null @@ -1,93 +0,0 @@ -/* - Object oriented design is commonly used in video games. For this part of the assignment - you will be implementing several classes with their correct inheritance heirarchy. - - In this file you will be creating three classes: - GameObject - createdAt - dimensions - destroy() // prototype method -> returns the string 'Game object was removed from the game.' - - NPC - hp - name - takeDamage() // prototype method -> returns the string ' took damage.' - // should inherit destroy() from GameObject's prototype - - Humanoid - faction - weapons - language - greet() // prototype method -> returns the string ' offers a greeting in .' - // should inherit destroy() from GameObject through NPC - // should inherit takeDamage() from NPC - - Inheritance chain: Humanoid -> NPC -> GameObject - Instances of Humanoid should have all of the same properties as NPC and GameObject. - Instances of NPC should have all of the same properties as GameObject. - - Example: - - const hamsterHuey = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 1, - height: 1, - }, - hp: 5, - name: 'Hamster Huey', - faction: 'Gooey Kablooie', - weapons: [ - 'bubblegum', - ], - language: 'Hamsterish', - }); - - hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' - hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' - hamsterHuey.destroy(); // returns 'Game object was removed from the game.' -*/ - -function GameObject(options) { - this.createdAt = options.createdAt; - this.dimensions = options.dimensions; -} - -GameObject.prototype.destroy = function destroy() { - return 'Game object was removed from the game.'; -}; - -function NPC(options) { - GameObject.call(this, options); - this.hp = options.hp; - this.name = options.name; -} - -NPC.prototype = Object.create(GameObject.prototype); - -NPC.prototype.takeDamage = function takeDamage() { - return `${this.name} took damage.`; -}; - -function Humanoid(options) { - NPC.call(this, options); - this.faction = options.faction; - this.weapons = options.weapons; - this.language = options.language; -} - -Humanoid.prototype = Object.create(NPC.prototype); - -Humanoid.prototype.greet = function greet() { - return `${this.name} offers a greeting in ${this.language}.`; -}; - -module.exports = { - GameObject, - NPC, - Humanoid, -}; - - - diff --git a/src/prototype.js b/src/prototype.js index e2494a6..f2825e2 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -49,7 +49,39 @@ hamsterHuey.destroy(); // returns 'Game object was removed from the game.' */ -/* eslint-disable no-undef */ +function GameObject(options) { + this.createdAt = options.createdAt; + this.dimensions = options.dimensions; +} + +GameObject.prototype.destroy = function destroy() { + return 'Game object was removed from the game.'; +}; + +function NPC(options) { + GameObject.call(this, options); + this.hp = options.hp; + this.name = options.name; +} + +NPC.prototype = Object.create(GameObject.prototype); + +NPC.prototype.takeDamage = function takeDamage() { + return `${this.name} took damage.`; +}; + +function Humanoid(options) { + NPC.call(this, options); + this.faction = options.faction; + this.weapons = options.weapons; + this.language = options.language; +} + +Humanoid.prototype = Object.create(NPC.prototype); + +Humanoid.prototype.greet = function greet() { + return `${this.name} offers a greeting in ${this.language}.`; +}; module.exports = { GameObject,