diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..f50224a8d 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -15,6 +15,15 @@ * 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 (mainat) { + this.createdAt = mainat.createdAt; + this.dimensions = mainat.dimensions; + this.name = mainat.name; +}; + GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game.`; + }; + /* === CharacterStats === @@ -22,6 +31,16 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(mainat) { + GameObject.call(this, mainat); + this.healthPoints = mainat.healthPoints; + this.name = mainat.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.) === @@ -32,6 +51,18 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ +function Humanoid(mainat) { + CharacterStats.call(this, mainat); + this.team = mainat.team; + this.weapons = mainat.weapons; + this.language = mainat.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 +72,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 +133,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..8d480bd50 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,7 +1,7 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. +* 1. Window Binding is when no other rules are present the "this" keyword will automatically implement window object, if strict mode is active it will default undefined instead. * 2. * 3. * 4. @@ -10,17 +10,49 @@ */ // Principle 1 +function hotWheel(monster){ + console.log(`I use to love` + `${this.monster}` + ` trucks`); + return monster; + +} +hotWheel(this); // code example for Window Binding // Principle 2 +let jay = { + name: "Jarrod", + says: "what's up guys!", + jaySays: function (){ + console.log(this.says) + } +} + +jay.jaySays(); // code example for Implicit Binding // Principle 3 +function fastCar(brz){ + this.thing = brz; +} + +let Car = new fastCar('brz goes 0-60 in 6.2 sec'); +console.log(Car.thing); // code example for New Binding // Principle 4 +function car(){ + console.log(this.fast); +} + +let wrx = { + brand: "subaru", + fast: "0-60 in 5.5 seconds" +} + +car.call(wrx) + // code example for Explicit Binding \ No newline at end of file