diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..1bbdd8283 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -15,6 +15,14 @@ * dimensions (These represent the character's size in the video game) * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(attributes){ + this.createdAt = attributes.createdAt, + this.name = attributes.name, + this.dimensions =attributes.dimensions + } + GameObject.prototype.destroy = function(){ + return `${this.name} was removed from the game.` + }; /* === CharacterStats === @@ -22,7 +30,18 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ - +function CharacterStats(attributes){ + GameObject.call(this, attributes), + this.healthPoints = attributes.healthPoints + + } + CharacterStats.prototype = GameObject.prototype; + CharacterStats.prototype.takeDamage = function(){ + return `${this.name} took damage.` + + }; + + /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -32,7 +51,62 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - + function Humanoid(attributes){ + CharacterStats.call(this, attributes), +this.team = attributes.team, +this.weapons = attributes.weapons, +this.language = attributes.language + + } + Humanoid.prototype = CharacterStats.prototype; +Humanoid.prototype.greet = function(){ + return `${this.name} offers a greeting in ${this.language}.` +}; + +function Villian(attributes) { + Humanoid.call(this, attributes), + this.name = attributes.name, + this.weapons = attributes.weapons, + this.damage = attributes.damage, + this.hp = attributes.hp + } + Villian.prototype = Object.create(Humanoid); + Villian.prototype.Damage = function (char){ + return `${this.name} attacks ${char.name} with a ${this.weapons} and did ${this.damage} damage to the hero! The hero now has ${char.hp-this.damage} health.` + }, + Villian.prototype.Destruction = function (char1, char2){ + if (this.hp <= 0){ + return `${this.name} has 0 hp! ${this.name} is now unconscious.` + }else if (this.hp < 5){ + return `${this.name} has ${this.hp}! They are very low on health!` + }else { return `currently ${char1.name} has ${char1.hp -char2.damage} health`} + + }; + + + + + function Hero(attributes){ + Humanoid.call(this, attributes), + this.name = attributes.name, + this.weapons = attributes.weapons, + this.damage = attributes.damage, + this.hp = attributes.hp + } + + Hero.prototype = Object.create(Villian); + let currentHealth2 = this.hp-Villian.damage; + Hero.prototype.Damage = function (char){ + return `${this.name} attacks the ${char.name} with a ${this.weapons} and did ${this.damage} damage to the villian! The Villian now has ${char.hp - this.damage} health.` + }; + Hero.prototype.Destruction = function (char1, char2){ + if (this.hp <= 0){ + return `${this.name} has 0 hp! ${this.name} is now unconscious.` + }else if (this.hp < 5){ + return `${this.name} has ${this.hp}! They are very low on health!` + }else { return `currently ${char1.name} has ${char1.hp -char2.damage} health`} + + }; /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. @@ -41,7 +115,7 @@ // Test you work by un-commenting these 3 objects and the list of console logs below: -/* + const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -91,7 +165,19 @@ ], language: 'Elvish', }); - + const Snyder = new Villian({ + name: 'Snyder the Villian', + weapons: 'Sythe of Loathing', + damage: 25, + hp: 250 + }); + + const Stan = new Hero({ + name: 'Stan the Hero', + weapons: 'Lance of Light', + damage: 30, + hp: 180 + }); console.log(mage.createdAt); // Today's date console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } console.log(swordsman.healthPoints); // 15 @@ -102,7 +188,10 @@ 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. -*/ + console.log(Snyder.Damage(Stan)); + console.log(Snyder.Destruction(Stan, Snyder)); + console.log(Stan.Damage(Snyder)); + console.log(Stan.Destruction(Snyder, Stan)); // Stretch task: // * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..4f2a125c0 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,10 +1,10 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. window binding- this defaults to the window that you are in since it has no other reference +* 2. implicit binding- this refers to what is left of the dot +* 3. new binding- makes a new object that is based on a different object, this will refer to the new object +* 4. explicit binding- explicitly telling this what it is to point to using call apply or bind * * write out a code example of each explanation above */ @@ -12,15 +12,44 @@ // Principle 1 // code example for Window Binding +console.log(this) // Principle 2 // code example for Implicit Binding - +const person = { + name: 'Aiden', + age: 27, + greet() { + console.log(`Hello, my name is ${this.name}`) + } + } +person.greet(); // Principle 3 // code example for New Binding +function personData (name, age, location){ +this.name = name, +this.age = age, +this.location = location +}; + +const NewPersonData = new personData('Tyler', 32, 'South Africa'); +const NewPersonData2 = new personData('Harry', 19, 'China'); + +console.log(`The first person that acessed this computer was ${NewPersonData.name}, age: ${NewPersonData.age}, location: ${NewPersonData.location} and the second person that acessed this computer was ${NewPersonData2.name}, age: ${NewPersonData2.age}, location: ${NewPersonData2.location} `); + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +var person2 = { + fullName: function(city, country) { + console.log(this.firstName + " " + this.lastName + "," + city + "," + country) + } + } + var person1 = { + firstName:"John", + lastName: "Green" + } + person2.fullName.apply(person1, ["Danville", "Ky"]);