From 7a53fa5b6f9677094c9d3c468a1fc170aa6084b8 Mon Sep 17 00:00:00 2001 From: josh Hill Date: Wed, 16 Oct 2019 16:36:53 -0400 Subject: [PATCH 1/2] MVP --- assignments/prototypes.js | 71 ++++++++++++++++++++++++++++++++++++--- assignments/this.js | 59 +++++++++++++++++++++++++++++--- 2 files changed, 120 insertions(+), 10 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..4a6b12b04 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -7,7 +7,37 @@ Each constructor function has unique properties and methods that are defined in their block comments below: */ - + 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.`; + } + + + function CharacterStats(attributes){ + this.healthPoints = attributes.healthPoints; + GameObject.call(this, attributes); + } + CharacterStats.prototype = Object.create(GameObject.prototype) + CharacterStats.prototype.takeDamage = function (){ + return `${this.name} took damage.`; + } + + +function Humanoid(attributes){ + this.team = attributes.team; + this.weapons = attributes.weapons; + this.language = attributes.language; + CharacterStats.call(this, attributes); +} +Humanoid.prototype = Object.create(CharacterStats.prototype); +Humanoid.prototype.greet = function(){ + return `${this.name} offers a greeting in ${this.language}`; +} /* === GameObject === * createdAt @@ -41,7 +71,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: { @@ -97,14 +127,45 @@ console.log(swordsman.healthPoints); // 15 console.log(mage.name); // Bruce console.log(swordsman.team); // The Round Table - console.log(mage.weapons); // Staff of Shamalama + console.log(mage.weapons[0]); // Staff of Shamalama console.log(archer.language); // Elvish 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 constructor functions that inherit from the Humanoid constructor function. // * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0; - // * Create two new objects, one a villain and one a hero and fight it out with methods! \ No newline at end of file + // * Create two new objects, one a villain and one a hero and fight it out with methods! + + + + +// function Hero(attributes){ +// Humanoid.call(this, attributes); +// } +// Hero.prototype.removeHP = function(){ +// this.healthPoints--; +// console.log(`${this.name} took 1 dmg, new HP is ${this.healthPoints}`); +// if (this.healthPoints === 0){ +// return this.destroy(); +// } +// } + + +// const superMan = new Hero({ +// createdAt: new Date(), +// dimensions: { +// length: 2, +// width: 1, +// height: 1, +// }, +// healthPoints: 1, +// name: 'Bruce', +// team: 'Mage Guild', +// weapons: [ +// 'Staff of Shamalama', +// ], +// language: 'Common Tongue', +// }); diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..3ad7bd622 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,10 +1,13 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. WINDOW/GLOBAL OBJECT BINDING: when using "this" in global scope you will return the ENTIRITY of global objects. + +* 2. IMPLICIT BINDING: "this" points to the object before the dot notation. + +* 3. NEW BINDING: Basically using a constructor function to return an object, you have to use the "new" keyword to create a new object. + +* 4. EXPLICIT BINDING: whenever this is defined, you can override the constructor by using keywords - bind, call, apply. * * write out a code example of each explanation above */ @@ -13,14 +16,60 @@ // code example for Window Binding + + +function global(name){ + console.log(this) + return name; +} + +// global("josh"); + + + // Principle 2 // code example for Implicit Binding + + +object = { + greeting: "hello", + sayHello: function(name){ + console.log(`${this.greeting} my name is ${name}`) + console.log(this) + } +} +object.sayHello("josh!!!!") + // Principle 3 // code example for New Binding + + + +function Object(greeter) { + + this.greeting = "hello"; + this.greeter = greeter; + this.sayHello = function(name){ + console.log(`${this.greeting} my name is ${this.greeter}`); + console.log(this); + } +} +const joshh = new Object("josh") +const tata = new Object ("tataaaaaaa") +joshh.sayHello(); +tata.sayHello(); + + // Principle 4 -// code example for Explicit Binding \ No newline at end of file + + +// code example for Explicit Binding + + + +joshh.sayHello.call(tata); \ No newline at end of file From 7adc7585ffb6a37040a9119485d1c9daa4134a0c Mon Sep 17 00:00:00 2001 From: josh Hill Date: Wed, 16 Oct 2019 16:49:08 -0400 Subject: [PATCH 2/2] Stretch --- assignments/prototypes.js | 90 +++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 4a6b12b04..6bd5f6aad 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -142,30 +142,66 @@ Humanoid.prototype.greet = function(){ -// function Hero(attributes){ -// Humanoid.call(this, attributes); -// } -// Hero.prototype.removeHP = function(){ -// this.healthPoints--; -// console.log(`${this.name} took 1 dmg, new HP is ${this.healthPoints}`); -// if (this.healthPoints === 0){ -// return this.destroy(); -// } -// } - - -// const superMan = new Hero({ -// createdAt: new Date(), -// dimensions: { -// length: 2, -// width: 1, -// height: 1, -// }, -// healthPoints: 1, -// name: 'Bruce', -// team: 'Mage Guild', -// weapons: [ -// 'Staff of Shamalama', -// ], -// language: 'Common Tongue', -// }); + function Hero(attributes){ + Humanoid.call(this, attributes); + } +Hero.prototype.removeHP = function(){ + this.healthPoints--; + console.log(`${this.name} took 1 dmg, new HP is ${this.healthPoints}`); + if (this.healthPoints === 0){ + return console.log(`you've been removed from the game`); + } +} + +function Villain(attributes){ + Humanoid.call(this, attributes); +} + +Villain.prototype.removeHP = function(){ + this.healthPoints--; + console.log(`${this.name} took 1 dmg, new HP is ${this.healthPoints}`); + if (this.healthPoints === 0){ + return console.log(`you've been removed from the game`); + } +} + +const superMan = new Hero({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + healthPoints: 3, + name: 'Bruce', + team: 'Mage Guild', + weapons: [ + 'Staff of Shamalama', + ], + language: 'Common Tongue', +}); + +const Venom = new Villain ({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + healthPoints: 3, + name: 'alexis', + team: 'Mage Guild', + weapons: [ + 'Staff of Shamalama', + ], + language: 'Common Tongue', +}); +superMan.removeHP(); +superMan.removeHP(); +superMan.removeHP(); + +Venom.removeHP(); +Venom.removeHP(); +Venom.removeHP(); + +