From 93e1b385711ce35789e336a6d6116ffd8e39a55c Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 11:18:18 -0700 Subject: [PATCH 01/12] gave an overarching definition of 'this' in my words and one for window binding --- assignments/this.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..074210387 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,7 +1,13 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. + + 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. * 3. * 4. From 371979509d98dfd125802397c8608cefe4c40237 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 12:07:02 -0700 Subject: [PATCH 02/12] wrote explanations of the 4 'this' principles --- assignments/this.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/assignments/this.js b/assignments/this.js index 074210387..6de267bdc 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -8,9 +8,11 @@ * 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. -* 3. -* 4. +* 2. Implicit Binding is the principle that if this is used in an object or 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 */ From 1a61170efa950d4b8dd7f17c0c53d726583582f1 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 12:11:12 -0700 Subject: [PATCH 03/12] added code as an example of window binding --- assignments/this.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/assignments/this.js b/assignments/this.js index 6de267bdc..fdca6ab92 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -19,6 +19,14 @@ // Principle 1 +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 + // code example for Window Binding // Principle 2 From 1e3e84e0c2957b3422529ac12189281cc2879d3f Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 12:50:59 -0700 Subject: [PATCH 04/12] added code as an example of implicit binding, new binding, and explicit binding --- assignments/this.js | 51 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/assignments/this.js b/assignments/this.js index fdca6ab92..4c23d0aca 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -8,7 +8,7 @@ * 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 object or objects method, than it points to that object itself. So 'this' will reference a property/key value pair within the 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. @@ -19,6 +19,8 @@ // Principle 1 +//code example for Window Binding + function atGlbl(){ console.log(this.world) }; @@ -27,16 +29,55 @@ const world = "won't be this"; atGlbl(); //browser seems to be defaulting "use strict" because undefined is coming back instead of window -// code example for Window Binding - // 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)); From 7edadc58b5b4047d5b3076b62964e1c722718308 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 13:00:27 -0700 Subject: [PATCH 05/12] made a GameObj constructor and a CharStats constructor, made a CharStats prototype Fn, nested CharStats in GameObj for inheritance, need to make destroy Fn a prototype Fn --- assignments/prototypes.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..5919131d0 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -16,6 +16,25 @@ * 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; + this.destroy = function(){ + return `${this.name} was removed from the game.`; + }; + + + function CharacterStats(stats){ + this.healthPoints = stats.healthPoints; + }//end of CharStats constructor + + CharacterStats.prototype.takeDamage = function(){ + return `${this.name} took damage.`; + };//1:1 CharacterStats prototype Fns + +}; //end of GameObject constructor + /* === CharacterStats === * healthPoints @@ -23,6 +42,8 @@ * should inherit destroy() from GameObject's prototype */ + + /* === Humanoid (Having an appearance or character resembling that of a human.) === * team From c6f597f05ee5ae72971cecc266ec96edec0975d9 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 13:02:50 -0700 Subject: [PATCH 06/12] made a GameObj prototype and added a this.destroy to CharStats hoping it will inherit it from its prototype --- assignments/prototypes.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5919131d0..1f906f3e7 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -20,13 +20,14 @@ function GameObject(props){ this.createdAt= props.createdAt; this.name= props.name; this.dimensions = props.dimensions; - this.destroy = function(){ + + GameObject.prototype.destroy = function(){ return `${this.name} was removed from the game.`; }; - function CharacterStats(stats){ this.healthPoints = stats.healthPoints; + this.destroy();//WILL THIS WORK??? }//end of CharStats constructor CharacterStats.prototype.takeDamage = function(){ From c2b091ad4f01a58c60e5ddba2bfe44571ab90a7a Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 13:06:55 -0700 Subject: [PATCH 07/12] added humanoid constructor and humanoid prototype Fn as nested elements in CharStats --- assignments/prototypes.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 1f906f3e7..cb3435a0f 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -27,7 +27,21 @@ function GameObject(props){ function CharacterStats(stats){ this.healthPoints = stats.healthPoints; - this.destroy();//WILL THIS WORK??? + this.destroy();//WILL THIS WORK???////////////////////////////////////// + + + function Humanoid(traits){ + this.destroy(); + this.takeDamage(); + this.team = traits.team; + this.weapons = traits.weapons; + this.language = traits.language; + } // end of humanoid constructor + + Humanoid.prototype.greet = function(){ + returns `${this.name} offers a greeting in ${this.language}.` + } + }//end of CharStats constructor CharacterStats.prototype.takeDamage = function(){ From 400963a34f5363ba3a559a857bbd94e5dd36fa90 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 13:16:07 -0700 Subject: [PATCH 08/12] the constructor Fns didn't need to be nested- fixed, the upper level constructors had to be called by the lower - fixed --- assignments/prototypes.js | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index cb3435a0f..c77046fde 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -25,31 +25,31 @@ function GameObject(props){ return `${this.name} was removed from the game.`; }; - function CharacterStats(stats){ - this.healthPoints = stats.healthPoints; - this.destroy();//WILL THIS WORK???////////////////////////////////////// - function Humanoid(traits){ - this.destroy(); - this.takeDamage(); - this.team = traits.team; - this.weapons = traits.weapons; - this.language = traits.language; - } // end of humanoid constructor +}; //end of GameObject constructor - Humanoid.prototype.greet = function(){ - returns `${this.name} offers a greeting in ${this.language}.` - } +function CharacterStats(stats){ + GameObject.call(this, stats); + this.healthPoints = stats.healthPoints; - }//end of CharStats constructor - - CharacterStats.prototype.takeDamage = function(){ - return `${this.name} took damage.`; - };//1:1 CharacterStats prototype Fns +}//end of CharStats constructor -}; //end of GameObject constructor +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.greet = function(){ + returns `${this.name} offers a greeting in ${this.language}.` +} /* === CharacterStats === * healthPoints @@ -77,7 +77,7 @@ function GameObject(props){ // Test you work by un-commenting these 3 objects and the list of console logs below: -/* + const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -138,7 +138,7 @@ function GameObject(props){ 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. From c4c4420de799b286ec8b9de913e35cf3533b641b Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 13:16:56 -0700 Subject: [PATCH 09/12] fixed returns to return in humanoid proto --- assignments/prototypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index c77046fde..55245268f 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -48,7 +48,7 @@ function Humanoid(traits){ } // end of humanoid constructor Humanoid.prototype.greet = function(){ - returns `${this.name} offers a greeting in ${this.language}.` + return `${this.name} offers a greeting in ${this.language}.` } /* === CharacterStats === From 5ee2cde358ed5d335d745497f41c02c810366b17 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 13:31:46 -0700 Subject: [PATCH 10/12] takeDamage and Destroy Fns weren't working because I needed two different Object.create for the constructors to inherit prototype functions, couldn't write 2 humanoid object.creates because one would overwrite the other, also, order/placement of those statements is important - they should come after the constructors but before the constructors prototype Fns --- assignments/prototypes.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 55245268f..33089cd04 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -35,6 +35,8 @@ function CharacterStats(stats){ }//end of CharStats constructor +CharacterStats.prototype = Object.create(GameObject.prototype); + CharacterStats.prototype.takeDamage = function(){ return `${this.name} took damage.`; };//1:1 CharacterStats prototype Fns @@ -42,14 +44,21 @@ CharacterStats.prototype.takeDamage = function(){ 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}.` } + + /* === CharacterStats === * healthPoints From 50ff7b17033486d97e7d35f06ff8ddcfac9a1e11 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 14:01:38 -0700 Subject: [PATCH 11/12] Post MVP: added Hero and Villian constructor Fns and made their prototypes use Humanoid.prototype as a basis --- assignments/prototypes.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 33089cd04..a144826f9 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -58,6 +58,20 @@ Humanoid.prototype.greet = function(){ return `${this.name} offers a greeting in ${this.language}.` } +function Hero(virtues){ + Humanoid.call(this, virtues); + +}//end of hero constructor + +Hero.prototype = Object.create(Humanoid.prototype); + +function Villian(vices){ + Humanoid.call(this, vices); + +} + +Villian.prototype = Object.create(Humanoid.prototype); + /* === CharacterStats === From cbbee22fe6b8a430ba89c47eba6a097ce7da1492 Mon Sep 17 00:00:00 2001 From: CJ Lucido Date: Wed, 18 Sep 2019 14:28:16 -0700 Subject: [PATCH 12/12] a hero and villian are added to the game, inheriting from humanoids. They battle on the console. --- assignments/prototypes.js | 55 ++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index a144826f9..82506f271 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -30,12 +30,12 @@ function GameObject(props){ }; //end of GameObject constructor function CharacterStats(stats){ - GameObject.call(this, stats); + GameObject.call(this, stats);//inherits from the higher constructor this.healthPoints = stats.healthPoints; }//end of CharStats constructor -CharacterStats.prototype = Object.create(GameObject.prototype); +CharacterStats.prototype = Object.create(GameObject.prototype); //inherits/creates from the higher prototype CharacterStats.prototype.takeDamage = function(){ return `${this.name} took damage.`; @@ -60,17 +60,21 @@ Humanoid.prototype.greet = function(){ 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 Villian(vices){ +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.` + }; } -Villian.prototype = Object.create(Humanoid.prototype); +Villain.prototype = Object.create(Humanoid.prototype); /* @@ -151,6 +155,41 @@ Villian.prototype = Object.create(Humanoid.prototype); 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 @@ -161,7 +200,9 @@ Villian.prototype = Object.create(Humanoid.prototype); 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.