diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..fee20af44 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -16,6 +16,14 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(gameProp) { + this.createdAt = gameProp.createdAt, + this.dimensions = gameProp.dimensions +} +GameObject.prototype.destroy = function () { + return `${this.name} was removed from the database.`; +} + /* === CharacterStats === * healthPoints @@ -23,16 +31,40 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(charProp) { + GameObject.call(this, charProp) + this.healthPoints = charProp.healthPoints, + this.name = charProp.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 * weapons * language * greet() // prototype method -> returns the string ' offers a greeting in .' - * should inherit destroy() from GameObject through CharacterStats + * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - + +function Humanoid(humanProp) { + CharacterStats.call(this, humanProp) + this.team = humanProp.team; + this.weapons = humanProp.weapons; + this.language = humanProp.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. @@ -41,7 +73,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 +134,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..dfa0a156e 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. Globa/Window binding object: When in the global scope, the value of “this” will be the window/console Object +* 2. Implicit Binding: Whenever a function is called by a preceding dot, the object before that dot is this. +* 3. New Binding: Whenever a constructor function is used, this refers to the specific instance of the object that is created and returned by the constructor function. +* 4. Explicit Binding: Whenever JavaScript’s call or apply method is used, this is explicitly defined. * * write out a code example of each explanation above */ @@ -12,10 +12,24 @@ // Principle 1 // code example for Window Binding +function sayCity(city) { + return city; +} +sayCity("Miami-Beach"); + + // Principle 2 // code example for Implicit Binding +const myGreeting = { + greeting: 'Hello', + sayHi: function(name) { + console.log(`${this.greeting} my name is ${name}`); + + } +}; +myGreeting.sayHi("Jesus"); // Principle 3