From f48311cbdb2889f6fef2eeeafc5fb1f2cb75ed57 Mon Sep 17 00:00:00 2001 From: Greg Hayes Date: Wed, 18 Sep 2019 22:43:24 -0500 Subject: [PATCH 1/4] Initial commit, Humanoid constructor working --- assignments/prototypes.js | 154 +++++++++++++++++++++----------------- 1 file changed, 85 insertions(+), 69 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..deb4e6794 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -7,14 +7,24 @@ Each constructor function has unique properties and methods that are defined in their block comments below: */ - + /* === GameObject === * createdAt * name * 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(attribute) { + this.newCreated = attribute.createdAt; + this.newName = attribute.name; + this.newDimension = attribute.dimension; + this.destroy = function() { + return `${this.newName} was removed from the game.`; + }; + // console.log(this); +} /* === CharacterStats === @@ -22,6 +32,13 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(stat) { + this.newHealthPoints = stat.healthPoints; + this.takeDamage = function() { + return `${this.newName} took damage.`; + }; + // console.log(this); +} /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -32,79 +49,78 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - +function Humanoid(profile) { + this.newTeam = profile.team; + this.newWeapon = profile.weapons; + this.newLanguage = profile.language; + this.greet = function() { + return `${this.newName} offers a greeting in ${this.newLanguage}.`; + }; + console.log(this); +} + /* - * 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. -*/ + * 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: { - length: 2, - width: 1, - height: 1, - }, - healthPoints: 5, - name: 'Bruce', - team: 'Mage Guild', - weapons: [ - 'Staff of Shamalama', - ], - language: 'Common Tongue', - }); +const mage = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1 + }, + healthPoints: 5, + name: "Bruce", + team: "Mage Guild", + weapons: ["Staff of Shamalama"], + language: "Common Tongue" +}); - const swordsman = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 2, - height: 2, - }, - healthPoints: 15, - name: 'Sir Mustachio', - team: 'The Round Table', - weapons: [ - 'Giant Sword', - 'Shield', - ], - language: 'Common Tongue', - }); +const swordsman = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 2, + height: 2 + }, + healthPoints: 15, + name: "Sir Mustachio", + team: "The Round Table", + weapons: ["Giant Sword", "Shield"], + language: "Common Tongue" +}); - const archer = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 1, - width: 2, - height: 4, - }, - healthPoints: 10, - name: 'Lilith', - team: 'Forest Kingdom', - weapons: [ - 'Bow', - 'Dagger', - ], - language: 'Elvish', - }); +const archer = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4 + }, + healthPoints: 10, + name: "Lilith", + team: "Forest Kingdom", + weapons: ["Bow", "Dagger"], + language: "Elvish" +}); - console.log(mage.createdAt); // Today's date - console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } - console.log(swordsman.healthPoints); // 15 - console.log(mage.name); // Bruce - console.log(swordsman.team); // The Round Table - console.log(mage.weapons); // Staff of Shamalama - console.log(archer.language); // Elvish - 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(mage.createdAt); // Today's date +console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } +console.log(swordsman.healthPoints); // 15 +console.log(mage.name); // Bruce +console.log(swordsman.team); // The Round Table +console.log(mage.weapons); // Staff of Shamalama +console.log(archer.language); // Elvish +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. - // * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0; - // * Create two new objects, one a villain and one a hero and fight it out with methods! \ No newline at end of file +// Stretch task: +// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function. +// * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0; +// * Create two new objects, one a villain and one a hero and fight it out with methods! From 81572cf15cbaeda4db45020db571537873a9da08 Mon Sep 17 00:00:00 2001 From: Greg Hayes Date: Thu, 19 Sep 2019 11:00:34 -0500 Subject: [PATCH 2/4] Got prototypes to work, troubleshooting properties --- assignments/prototypes.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index deb4e6794..bb0bb3692 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -19,27 +19,39 @@ function GameObject(attribute) { this.newCreated = attribute.createdAt; this.newName = attribute.name; - this.newDimension = attribute.dimension; - this.destroy = function() { - return `${this.newName} was removed from the game.`; - }; + this.newDimensions = attribute.dimensions; + + //*************************The below function can be abstracted with a protoype *****************/ + // this.destroy = function() { + // return `${this.newName} was removed from the game.`; + //}; // console.log(this); } +GameObject.prototype.destroy = function() { + // Protoype + return `${this.newName} was removed from the game.`; +}; + /* === CharacterStats === * healthPoints * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ -function CharacterStats(stat) { - this.newHealthPoints = stat.healthPoints; - this.takeDamage = function() { - return `${this.newName} took damage.`; - }; + +function CharacterStats(stats) { + GameObject.call(this, stats); + this.newHealthPoints = stats.healthPoints; // console.log(this); } +CharacterStats.prototype = Object.create(GameObject.prototype); + +CharacterStats.prototype.takeDamage = function() { + // Protoype + return `${this.newName} took damage.`; +}; /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -50,15 +62,18 @@ function CharacterStats(stat) { * should inherit takeDamage() from CharacterStats */ function Humanoid(profile) { + CharacterStats.call(this, profile); this.newTeam = profile.team; this.newWeapon = profile.weapons; this.newLanguage = profile.language; this.greet = function() { return `${this.newName} offers a greeting in ${this.newLanguage}.`; }; - console.log(this); + // console.log(this); } +Humanoid.prototype = Object.create(CharacterStats.prototype); + /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. @@ -109,6 +124,7 @@ const archer = new Humanoid({ language: "Elvish" }); +console.log(mage.newTeam); console.log(mage.createdAt); // Today's date console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } console.log(swordsman.healthPoints); // 15 From fb784e5adfae41b65ed8dd2d45c03447e95b0bef Mon Sep 17 00:00:00 2001 From: Greg Hayes Date: Thu, 19 Sep 2019 11:04:41 -0500 Subject: [PATCH 3/4] protoype.js working as expected! --- assignments/prototypes.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index bb0bb3692..b6bb2de05 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -17,9 +17,9 @@ * */ function GameObject(attribute) { - this.newCreated = attribute.createdAt; - this.newName = attribute.name; - this.newDimensions = attribute.dimensions; + this.createdAt = attribute.createdAt; + this.name = attribute.name; + this.dimensions = attribute.dimensions; //*************************The below function can be abstracted with a protoype *****************/ // this.destroy = function() { @@ -30,7 +30,7 @@ function GameObject(attribute) { GameObject.prototype.destroy = function() { // Protoype - return `${this.newName} was removed from the game.`; + return `${this.name} was removed from the game.`; }; /* @@ -42,7 +42,7 @@ GameObject.prototype.destroy = function() { function CharacterStats(stats) { GameObject.call(this, stats); - this.newHealthPoints = stats.healthPoints; + this.healthPoints = stats.healthPoints; // console.log(this); } @@ -50,7 +50,7 @@ CharacterStats.prototype = Object.create(GameObject.prototype); CharacterStats.prototype.takeDamage = function() { // Protoype - return `${this.newName} took damage.`; + return `${this.name} took damage.`; }; /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -63,9 +63,9 @@ CharacterStats.prototype.takeDamage = function() { */ function Humanoid(profile) { CharacterStats.call(this, profile); - this.newTeam = profile.team; - this.newWeapon = profile.weapons; - this.newLanguage = profile.language; + this.team = profile.team; + this.weapons = profile.weapons; + this.language = profile.language; this.greet = function() { return `${this.newName} offers a greeting in ${this.newLanguage}.`; }; @@ -124,7 +124,6 @@ const archer = new Humanoid({ language: "Elvish" }); -console.log(mage.newTeam); console.log(mage.createdAt); // Today's date console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } console.log(swordsman.healthPoints); // 15 From 3a4f1ce8af3c6147f355b4a7e0b47f073f208dee Mon Sep 17 00:00:00 2001 From: Greg Hayes Date: Thu, 19 Sep 2019 16:23:24 -0500 Subject: [PATCH 4/4] complete --- assignments/prototypes.js | 19 ++++++++----- assignments/this.js | 59 +++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index b6bb2de05..f6e075fcc 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -21,11 +21,10 @@ function GameObject(attribute) { this.name = attribute.name; this.dimensions = attribute.dimensions; - //*************************The below function can be abstracted with a protoype *****************/ + //******** The below function can be abstracted with a prototype ********/ // this.destroy = function() { // return `${this.newName} was removed from the game.`; //}; - // console.log(this); } GameObject.prototype.destroy = function() { @@ -43,9 +42,9 @@ GameObject.prototype.destroy = function() { function CharacterStats(stats) { GameObject.call(this, stats); this.healthPoints = stats.healthPoints; - // console.log(this); } +//Object.create() allows the constructor function to see/inherit a parent function CharacterStats.prototype = Object.create(GameObject.prototype); CharacterStats.prototype.takeDamage = function() { @@ -66,14 +65,20 @@ function Humanoid(profile) { this.team = profile.team; this.weapons = profile.weapons; this.language = profile.language; - this.greet = function() { - return `${this.newName} offers a greeting in ${this.newLanguage}.`; - }; - // console.log(this); + + // *** Again abstracting with a prototype below***// + // this.greet = function() { + // return `${this.newName} offers a greeting in ${this.newLanguage}.`; + // }; } +//Object.create() allows the constructor function to see/inherit a parent function Humanoid.prototype = Object.create(CharacterStats.prototype); +Humanoid.prototype.greet = function() { + return `${this.newName} offers a greeting in ${this.newLanguage}.`; +}; + /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..79d2eb4a9 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,57 @@ /* The for principles of "this"; -* in your own words. explain the four principle for the "this" keyword below. -* -* 1. -* 2. -* 3. -* 4. -* -* write out a code example of each explanation above -*/ + * in your own words. explain the four principle for the "this" keyword below. + * + * 1. this is applied to Window object by default is if has no other context. + * 2. this is applied to whatever object is left of the '.' when we invoke the function + * 3. when a function is invoked w/ the 'new' keyword, this points to the new function + * 4. You can use .call / .apply / .bind to tell this the function to work from + * + * write out a code example of each explanation above + */ // Principle 1 - // code example for Window Binding +function Person(attribute) { + // this.name = attribute.name; + console.log(this.says); +} +Person(); -// Principle 2 +const says = "Hello"; +// Principle 2 // code example for Implicit Binding +const user1 = { + name: "Greg", + greet() { + return `Hi, my name is ${this.name}.`; + } +}; +//this.name will use const user as its context +console.log(user1.greet()); // Principle 3 - // code example for New Binding - +const user2 = new Person({ + location: "Austin, TX", + tell() { + return `I live in ${this.location}.`; + } +}); +console.log(user2); // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +function GameObject(attribute) { + this.createdAt = attribute.createdAt; + this.name = attribute.name; + this.dimensions = attribute.dimensions; +} + +//Using .call this function now inherits from the GameObject constructor +function CharacterStats(stats) { + GameObject.call(this, stats); + this.healthPoints = stats.healthPoints; +} + +console.log(CharacterStats());