diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..14b8cc88b 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -1,21 +1,31 @@ /* - Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy. - + Object oriented design is commonly used in video games. + For this part of the assignment you will be implementing several constructor functions + with their correct inheritance hierarchy. In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid. - - At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions. + At the bottom of this file are 3 objects that all end up inheriting from Humanoid. + Use the objects at the bottom of the page to test your constructor functions. Each constructor function has unique properties and methods that are defined in their block comments below: -*/ - +*/ /* === GameObject === * createdAt * name * dimensions (These represent the character's size in the video game) - * destroy() // prototype method that returns: `${this.name} was removed from the game.` + * destroy() // prototype method that returns: '{this.name} was removed from the game.' */ +function GameObject(attr){ + this.createdAt = attr.createdAt; + this.name = attr.name; + this.dimensions = attr.dimensions; +} + +GameObject.prototype.destroy = function(){ + return `${this.name} was removed from the game.` +} + /* === CharacterStats === * healthPoints @@ -23,6 +33,18 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(stats){ + GameObject.call(this, stats); + this.healthPoints = stats.healthPoints; + this.name = stats.name; +} + +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.) === * team @@ -32,6 +54,19 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ + +function Humanoid(about){ + CharacterStats.call(this, about); + this.team = about.team; + this.weapons = about.weapons; + this.language = about.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 @@ -41,7 +76,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: { @@ -102,7 +137,7 @@ 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. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..09eec03c1 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,58 @@ /* 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 + * in your own words. explain the four principle for the "this" keyword below. + * + * 1. Window - works in global scope, "this" refers to window or console object. + + * 2. Implicit - only communicates inside of the function/object that it lives in. + * "this" points to what comes to the left of the dot. + + * 3. New - works with constructor functions, constructors always have a capital letter. + "this" refers to the new object created. + + * 4. Explicit - "this" is defined when the call or apply method is used. + * + * write out a code example of each explanation above + */ + + + // Principle 1 // code example for Window Binding + console.log(this); //"this" refers to the global scope + + // Principle 2 // code example for Implicit Binding +const jessNOV = { + name: 'Jess', + color: 'orange', + food: 'Thanksgiving dinner', + favoriteColor: function() { + console.log(`${this.name}'s favorite color is ${this.color}.`) + } + } + + jessNOV.favoriteColor(); //"this" refers to name and color (left of the dots) // Principle 3 // code example for New Binding +function Person(name, color) { + this.name = name; + this.color = color; + } + + const jane = new Person('jane doe', 'yellow'); + + console.log(jane); //"this" refers to Person // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +let favoriteFood = function() { + console.log(`${this.name}'s favorite food is ${this.food}.`) + } + + favoriteFood.call(jessNOV); //"this" refers to jessNOV \ No newline at end of file