diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..6b09fe5d6 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -8,6 +8,16 @@ Each constructor function has unique properties and methods that are defined in their block comments below: */ + +function GameObject(attr){ + this.createdAt = attr.createdAt, + this.name = attr.name, + this.dimensions = attr.dimensions + this.destroy = function(){ + return (`${this.name} was removed from the game.`); + }; +} + /* === GameObject === * createdAt @@ -16,13 +26,34 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function CharacterStats(atts){ + this.healthPoints = atts.healthPoints; + GameObject.call(this, atts) + this.takeDamage = function(){ + return(`${this.name} took damage`); + + }; +} + + /* === CharacterStats === * healthPoints * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function Humanoid(attx){ + this.team = attx.team, + this.weapons = attx.weapons, + this.language = attx.language + CharacterStats.call(this, attx) + GameObject.call(this, attx) + this.greet = function(){ + return (`${this.name} offers a greeting in ${this.language}.`); + +}; +}; /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -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..59d503ce2 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. Implicit Binding - this referes to what is left of the '.' when the fuction is called. +* 2. Explicit Binding - you state what the this keyword will be in the function +* 3. New Binding - when a function is call an function with the key work new in front it creats a new opject. +* 4. Window Binding - the this keyword refers to the window object * * write out a code example of each explanation above */ @@ -12,15 +12,44 @@ // Principle 1 // code example for Window Binding +console.log(this); -// Principle 2 +// Principle 2 // code example for Implicit Binding +const roll = function(person){ + person.rollCall = function(){ + console.log(this.name) + } +} +const aasa ={ + name: 'Aasa', +} +roll(aasa); +aasa.rollCall(); -// Principle 3 +// Principle 3 // code example for New Binding +const BackPack = function (color, pockets,){ + this.color = color; + this.pockets = pockets + this.thePack = (`The ${this.color} backpack has ${this.pockets} pockets.`) +}; + +const aasaBackPack = new BackPack('blue', 'ten') +console.log(aasaBackPack.thePack) + // Principle 4 +// code example for Explicit Binding + +var favTeam = function(sport, team){ + console.log(`Im ${this.name} and my favorite ${sport} team is the ${team}!`); +}; +var myName1 ={ + name: 'Aasa', +}; +var myTeam = ['football','Cowboys' ]; -// code example for Explicit Binding \ No newline at end of file +favTeam.apply(myName1, myTeam) \ No newline at end of file