diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..df7b601d3 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -15,14 +15,29 @@ * 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(attrs){ + this.createdAt = attrs.createdAt; + this.name = attrs.name; + this.dimensions = attrs.dimensions; +} +GameObject.prototype.destroy = function(){ + return `${this.name} was removed from the game.`; +} /* === CharacterStats === * healthPoints * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ - +function CharacterStats(childAttrs){ + GameObject.call(this,childAttrs); + this.healthPoints = childAttrs.healthpoints; +} +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,16 +47,29 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - + function Humanoid(humanAttrs){ + GameObject.call(this,humanAttrs); + CharacterStats.call(this,humanAttrs); + this.team = humanAttrs.team; + this.weapons = humanAttrs.weapons; + this.language = humanAttrs.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. * Instances of CharacterStats should have all of the same properties as GameObject. */ + + // 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 +130,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..ac318a07e 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,11 +1,17 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. +* 1. +Global Binding - when no other rules apply, the value of this is the Window. + * 2. +Implicit Binding - when the function is called and the object (this) is named to the left of a dot. + * 3. +New Binding - When a constructor function is used. + * 4. -* +*Explicit Binding - When a the call, apply or bind method is used to explicitly define 'this'. * write out a code example of each explanation above */ @@ -13,14 +19,48 @@ // code example for Window Binding +function sayMyName(myName) { + console.log(this); + return myName; + } + sayName("Katrina"); + // Principle 2 // code example for Implicit Binding +const greetingObj = { + greeting: 'Hey yall', + sayHello: function(myName) { + console.log(`${this.greeting} my name is ${name}`); + console.log(this); + } + }; + greetingObj.sayHello('Katrina'); + + + // Principle 3 // code example for New Binding +function NiceGirl(greeter) { + this.greeting = 'Hello '; + this.greeter = greeter; + this.speak = function() { + console.log(this.greeting + this.greeter); + console.log(this); + }; + } + + const beyonce = new NiceGirl('Beyonce'); + const kelly = new NiceGirl('Kelly'); + + beyonce.speak(); + kelly.speak(); + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding + +beyonce.speak.call(beyonce); \ No newline at end of file