diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 12ae352b1..0a8ba4ea5 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -14,6 +14,13 @@ * dimensions * destroy() // prototype method -> returns the string: 'Object 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 === @@ -22,6 +29,16 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats (charStats){ + GameObject.call(this, charStats); + this.hp = charStats.hp; + this.name = charStats.name; +} + +CharacterStats.prototype = Object.create(GameObject.prototype); +CharacterStats.prototype.takeDamage = function (){ + return (`${this.name} took damage.`); +} /* === Humanoid === @@ -32,6 +49,18 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ +function Humanoid(humanoidAttributes){ + CharacterStats.call(this, humanoidAttributes); + this.faction = humanoidAttributes.faction; + this.weapons = humanoidAttributes.weapons; + this.language = humanoidAttributes.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 +70,7 @@ // Test you work by uncommenting these 3 objects and the list of console logs below: -/* + const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -102,7 +131,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 Villian and Hero constructor functions that inherit from the Humanoid constructor function. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..ddb6b9ce4 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. Window Binding - this will point to the window or console object in the global scope +* 2. Implicit Binding - The dot that precedes the function that is called points to the object before the dot. +* 3. Explicit Binding - when call or apply or used, this is explicited +* 4. New Binding - when using a function to create an object, the new keyword is used and this will refer to the specific instance of the newly created object. * * write out a code example of each explanation above */ @@ -13,14 +13,52 @@ // code example for Window Binding +function windowName(name) { + console.log(this); + return name; +}; +windowName("hello"); + + // Principle 2 // code example for Implicit Binding +const hunter = { + traits: 'handsome', + + introduceYourself: function(name) { + console.log(`My name is ${name}, and I am ${this.traits}.`) + } +} +hunter.introduceYourself('Hunter'); + // Principle 3 // code example for New Binding +function BatmanAndRobin(dynamicDuo) { + this.greeting = 'I fight crime in a rubber suit, '; + this.dynamicDuo = dynamicDuo; + this.speak = function() { + + + console.log(this.greeting + this.dynamicDuo); + console.log(this); + }; + } + + const batman = new BatmanAndRobin('Robin'); + const robin = new BatmanAndRobin('Batman'); + +console.log(batman); +console.log(robin); + +batman.speak(); +robin.speak(); + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding + +batman.speak.call(robin); \ No newline at end of file