From d56bfd2e68ab08724c178fc7f5df43f6092c2502 Mon Sep 17 00:00:00 2001 From: Hanan Haji adow Date: Mon, 7 Nov 2022 22:59:47 -0700 Subject: [PATCH] making changes --- classes.js | 53 ++++++++++++++++++++++++++++++++++++++++----------- prototypes.js | 17 ++++++++++++++++- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/classes.js b/classes.js index 5f24389..c585a17 100644 --- a/classes.js +++ b/classes.js @@ -29,13 +29,25 @@ console.log("This is example result: ", fred.task()); // ++++ YOUR ASSIGNMENT STARTS HERE +++++ -/* - === GameObject === - * createdAt - * name - * dimensions (These represent the character's size in the video game) - * destroy() // A method that returns: `${this.name} was removed from the game.` -*/ + /* + === GameObject === + *createdAt + *name + *dimensions (These represent the character's size in the video game) + *destroy () // A method that returns: `${this.name} was removed from the game.` + */ + + class GameObject { + constructor(attributes) { + this.createdAt= attributes.createdAt; + this.name = attributes.name; + this.dimensions = attributes.dimensions; + } + destroy() { + return `${this.name} was removed from the game`; + } + +} /* === CharacterStats === @@ -44,6 +56,15 @@ console.log("This is example result: ", fred.task()); * should inherit destroy() from GameObject's method */ +class CharacterStats extends GameObject { + constructor(attributes){ + super(attributes); + this.healthPoints = attributes.healthPoints; + } + takeDamage(){ + return `${this.name} took damage`; + } +} /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -53,16 +74,26 @@ console.log("This is example result: ", fred.task()); * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - +class Humanoid extends CharacterStats{ + constructor(attributes){ + super(attributes); + this.team = attributes.team; + this.weapons = attributes.weapons; + this.language = attributes.language; + } + greet(){ + return ` ${this.name} offers a greening in ${this.language}`; + } +} /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. * Instances of CharacterStats should have all of the same properties as GameObject. */ -// Test you work by un-commenting these 3 objects and the list of console logs below: +// Test your work by un-commenting these 3 objects and the list of console logs below: + -/* const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -123,7 +154,7 @@ console.log("This is example result: ", fred.task()); console.log(archer.greet()); // Lilith offers a greeting in Elvish. console.log(mage.takeDamage()); // Bruce took damage. console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. -*/ + // Stretch task: // * Create Villain and Hero class that inherit from the Humanoid class. diff --git a/prototypes.js b/prototypes.js index ddc2e29..4cd6bda 100644 --- a/prototypes.js +++ b/prototypes.js @@ -5,7 +5,22 @@ // Give it "Name", "Age", "Hobby" properties // Also give it a method "Speak", which says "Hello, My name is + name + and I'm + age + years old. I love + Hobby" - +function Person(attributes){ + this.age = attributes.age; + this.name = attributes.name; + this.hobby =attributes.hobby; + // this.speak = functon(){ + // return `"Hello, My name is + name + and I'm + age + years old. I love + Hobby"`; + }; + Person.prototype.speak = function() { + return `"Hello, My name is ${this.name} and I'm ${this.age} age years old. i love ${this.hobby} Hobby"`; + }; + const me = new Person({ + name: "Hanan", + age: 25, + hobby: "relaxing and hanging out with friends", + }) + console.log(me.speak()); // Move the "Speak" method outside of the constructor function and into the prototype. // Create an object named "Me" using the "Person" constructor function // Call the "Speak" method on the "Me" object