diff --git a/classes.js b/classes.js index 1b13e77..01a1123 100644 --- a/classes.js +++ b/classes.js @@ -7,4 +7,5 @@ // Also give it a method "Speak", which says "Hello, My name is + name + and I'm + age + years old. I love + Hobby" -// Extend Person class and create one class for each member of your family. \ No newline at end of file +// Extend Person class and create one class for each member of your family. +// \ No newline at end of file diff --git a/prototypes.js b/prototypes.js index 5625c97..b3e7ad3 100644 --- a/prototypes.js +++ b/prototypes.js @@ -15,13 +15,29 @@ * 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.dimensions = attributes.dimensions; +} +GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game.` +} /* === CharacterStats === * healthPoints * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(attr) { + GameObject.call(this, attr) + this.name = attr.name; + this.healthPoints = attr.healthPoints; +} +CharacterStats.prototype = Object.create(GameObject.prototype); + +CharacterStats.prototype.takeDamage = function() { + return `${this.name} took damage.` +} /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -32,7 +48,17 @@ * 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 = Object.create(CharacterStats.prototype); + +Humanoid.prototype.greet = function() { + 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. @@ -107,4 +133,5 @@ // 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! + \ No newline at end of file diff --git a/this.js b/this.js index 969bbee..cf4b033 100644 --- a/this.js +++ b/this.js @@ -1,26 +1,61 @@ -/* The for principles of "this"; -* in your own words. explain the four principle for the "this" keyword below. -* -* 1. -* 2. -* 3. -* 4. -* -* write out a code example of each explanation above -*/ +// * The for principles of "this"; +// * in your own words. explain the four principle for the "this" keyword below. + +// * 1 +// * 2 +// * 3 +// * 4 + +// * write out a code example of each explanation above + // Principle 1 // code example for Window Binding +function dayOfTheWeek(date) { + console.log(this) +} +dayOfTheWeek("Wednesday"); // Principle 2 // code example for Implicit Binding - +const food = { + name: "Cheeseburger", + ingredients: ["Cheese", " Meat", " Lettuce", " Bacon"], + howToMake: function() { + console.log(`A ${this.name} has ${this.ingredients}`); + } +} + +food.howToMake(); // Principle 3 // code example for New Binding - +function BestJSFramework(framework) { + this.framework = framework; + this.end = "is the best framework, period!" + this.iAmBest = function() { + console.log(`${this.framework} ${this.end}`) + } +} + +const angular = new BestJSFramework("Angular"); +const react = new BestJSFramework("React"); + +angular.iAmBest(); +react.iAmBest(); // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +const city = { + name: "New York City" + } + + const neighborhoods = ["Upper West Side", "East Village", "Lower Manhattan"]; + +function neighborhoodsInCity(neighborhoods) { + console.log(`${neighborhoods} are located in ${this.name}`); + } + + neighborhoodsInCity.call(city, neighborhoods);