diff --git a/classes.js b/classes.js index 5f24389..c377102 100644 --- a/classes.js +++ b/classes.js @@ -37,6 +37,19 @@ console.log("This is example result: ", fred.task()); * 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 === * healthPoints @@ -44,6 +57,16 @@ console.log("This is example result: ", fred.task()); * should inherit destroy() from GameObject's method */ +class CharacterStats extends GameObject{ + constructor(ChildAttributes){ + super(ChildAttributes) + this.healthPoints=ChildAttributes.healthPoints; +} +takeDamage(){ + return `${this.name}took damage`; +} +} + /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -53,7 +76,18 @@ 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(Child2Attributes){ + super(Child2Attributes) + this.team=Child2Attributes.team; + this.weapons=Child2Attributes.weapons; + this.language=Child2Attributes.language; + + } + greet(){ + return ` ${this.name} offers a greeting in ${this.language}`; + } +} /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. @@ -62,7 +96,6 @@ console.log("This is example result: ", fred.task()); // Test you work by un-commenting these 3 objects and the list of console logs below: -/* const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -123,7 +156,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..bd7676e 100644 --- a/prototypes.js +++ b/prototypes.js @@ -5,6 +5,24 @@ // 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.Hubby =attributes.Hubby; + }; + + Person.prototype.speak = function() { + + return `Hello, My name is ${this.Name} and I'm ${this.Age} age years old. i love ${this.Hubby} Hobby`; + }; + + const Me = new Person({ + Name: "Iqra", + Age: 20, + Hubby: "reading", + + }) + 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