diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..acac2c7ef 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -1,13 +1,13 @@ /* 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. + 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. - + Each constructor function has unique properties and methods that are defined in their block comments below: */ - + /* === GameObject === * createdAt @@ -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(att1){ + this.createdAt = att1.createAt, + this.name = att1.name, + this.dimensions = att1.dimensions +} +GameObject.prototype.destroy = function () { + return `${this.name} was removed from the game.`; +} /* === CharacterStats === @@ -23,6 +31,15 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(Att2){ + this.healthPoints = Att2.healthPoints; + GameObject.call(this, Att2); +} +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,7 +49,18 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - +function Humanoid(att){ + this.team = att.team; + this.weapons = att.weapons; + this.language = att.language; + CharacterStats.call(this,att); +} + +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. @@ -41,7 +69,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,9 +130,9 @@ 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. + + // 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! diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..1fba1f883 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. +* 2. +* 3. +* 4. * * write out a code example of each explanation above */ @@ -12,15 +12,49 @@ // Principle 1 // code example for Window Binding +function x() { + console.log(this); + +} + +x(); // Principle 2 // code example for Implicit Binding +const person = { + firstName: 'john', + lastName: 'duo', + fun: function(){ + console.log(this.firstName); + } +} + +person.fun(); + // Principle 3 // code example for New Binding +function person2(talk){ + this.x = talk; +} + +let newPerson = new person2('I am a new person'); + +console.log(newPerson.x); // function is being invoked + + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +function any (){ + console.log(this.firstName); +} +let newAny = { + firstName : 'Avi', + lastName : 'Kandinov' +} +something = any.bind(newAny); +something();