diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..82506f271 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -16,6 +16,67 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(props){ + this.createdAt= props.createdAt; + this.name= props.name; + this.dimensions = props.dimensions; + + GameObject.prototype.destroy = function(){ + return `${this.name} was removed from the game.`; + }; + + + +}; //end of GameObject constructor + +function CharacterStats(stats){ + GameObject.call(this, stats);//inherits from the higher constructor + this.healthPoints = stats.healthPoints; + +}//end of CharStats constructor + +CharacterStats.prototype = Object.create(GameObject.prototype); //inherits/creates from the higher prototype + +CharacterStats.prototype.takeDamage = function(){ + return `${this.name} took damage.`; +};//1:1 CharacterStats prototype Fns + + +function Humanoid(traits){ + CharacterStats.call(this, traits); + + this.team = traits.team; + this.weapons = traits.weapons; + this.language = traits.language; +} // end of humanoid constructor + + +Humanoid.prototype = Object.create(CharacterStats.prototype); + + +Humanoid.prototype.greet = function(){ + return `${this.name} offers a greeting in ${this.language}.` +} + +function Hero(virtues){ + Humanoid.call(this, virtues); + this.divineMight = function(){ + return `${this.name} is immune to plights, makes it rain fire and radioactive light on opponent, destroying all villains` + }; +}//end of hero constructor + +Hero.prototype = Object.create(Humanoid.prototype); + +function Villain(vices){ + Humanoid.call(this, vices); + this.unrulyPlight = function(){ + return `A spell spoken in ${this.language} unleashes airbourne flesh eating bacteria, all but heros are taken to near death.` + }; +} + +Villain.prototype = Object.create(Humanoid.prototype); + + /* === CharacterStats === * healthPoints @@ -23,6 +84,8 @@ * should inherit destroy() from GameObject's prototype */ + + /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -41,7 +104,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: { @@ -92,6 +155,41 @@ language: 'Elvish', }); + const paladin = new Hero({ + createdAt: new Date(), + dimensions: { + length: 7, + width: 7, + height: 7, + }, + healthPoints: 1000, + name: 'Saint', + team: 'Higher Realms', + weapons: [ + 'Incapatability of presence', + 'Sword', + ], + language: 'Unknown- Alpha Hz', + }); + + const demon = new Villain({ + createdAt: new Date(), + dimensions: { + length: 6, + width: 6, + height: 6, + }, + healthPoints: 1, + name: 'Materiel', + team: 'Denser realms', + weapons: [ + 'Your vices', + 'Guilt', + 'Fear', + ], + language: 'Low Frequency', + }); + console.log(mage.createdAt); // Today's date console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } console.log(swordsman.healthPoints); // 15 @@ -102,7 +200,9 @@ 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. -*/ + console.log(demon.unrulyPlight()); + console.log(paladin.divineMight()); + console.log(demon.destroy()); // 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..4c23d0aca 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,83 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. + + For all forms of this I would say it is similar to the English use of it in comparison to 'that', that is 'this' points to an object that is NEAR us! If we were surrounded by only space then the concept of space would be the only object near us. In code it is like this but refers only to objects' attribute of containing things, so it points to the object nearest as a container. + + In technical terms, 'this' points to the parent scope or newly created parent scope of a container object, OR it will point to the object that you are calling or applying. + +* 1. Window binding is the principle that if a declared function or function expression was defined/assigned within the global scope and 'this was used in their definitions, then 'this' refers to the window (the actual JS language) or in node it points to the global object. + +* 2. Implicit Binding is the principle that if this is used in an objects method, than it points to that object itself. So 'this' will reference a property/key value pair within the object. + +* 3. New binding is the principle that when a constructor function is made using 'this' on the source objects key value pairs, then when a new object is created using that constructor function, 'this' will refer to that new object and not the source object or the constructor function. + +* 4. Explicit binding is the principle that, even if object properties are defined, an object's properties can be redefined/assigned to another object's properties using the .call or .apply methods. We can aslo use it to redefine/assign properties for a function using .bind. In other words, we can tell 'this' what it means. * * write out a code example of each explanation above */ // Principle 1 -// code example for Window Binding +//code example for Window Binding + +function atGlbl(){ + console.log(this.world) +}; + +const world = "won't be this"; + +atGlbl(); //browser seems to be defaulting "use strict" because undefined is coming back instead of window // Principle 2 // code example for Implicit Binding +let principal2 = { + name: "new object", + location: "place", + activity: function(){ + console.log(`${this.name} at ${this.location}`) + }, +}; + +principal2.activity(); + // Principle 3 -// code example for New Binding +//code example for New Binding + +function Principal3(props){ + this.name = props.name; + this.location = props.location; + this.year = props.year; + this.explain = function(){ + return `${this.name} was bound to her husband ${this.location} in ${this.year}` + } +}; + +const newlyWed = new Principal3( + { + name:"Mary Lee Bound", + location:"three steps down", + year:1999 + } + ); + +console.log(newlyWed.explain()); // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding + + + +const oldlyWed = new Principal3( + { + name:"Marie Turner Roun", + location:"back up town", + year: 1867 +} +); + +console.log(principal2.activity.call(oldlyWed));