From a7478408b297854a145dc5c540191518e0dee20d Mon Sep 17 00:00:00 2001 From: reggieTheCoder <33520187+reggieTheCoder@users.noreply.github.com> Date: Thu, 10 Oct 2019 17:24:20 -0400 Subject: [PATCH 1/4] this.js complete --- assignments/this.js | 56 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..9694d2df3 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. Window/Global Object Binding +* 2. Implicit Binding +* 3. Explicit Binding +* 4. New Binding * * write out a code example of each explanation above */ @@ -12,15 +12,55 @@ // Principle 1 // code example for Window Binding +function sayName(name) { + console.log(this); + return name; + } + sayName("Reginald Alford"); // Principle 2 - // code example for Implicit Binding +const sayNameFunc = obj => { + obj.sayName = function() { + return `Hello my name is ${this.name}`; + console.log(this); + }; + }; + const me = { name: 'Jason' }; + const you = { name: 'Freddy' }; + sayNameFunc(me); + sayNameFunc(you); + + // Invoke Methods on our objects + console.log(me.sayName()); + console.log(you.sayName()); // Principle 3 - // code example for New Binding - +function CordialPerson(greeter) { + this.greeting = 'Hello '; + this.greeter = greeter; + this.speak = function() { + console.log(this.greeting + this.greeter); + // console.log(this); + }; + } + + const jerry = new CordialPerson('Newman'); + const newman = new CordialPerson('Jerry'); + + jerry.speak(); + newman.speak(); // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +function greet () { + alert(`Hello, my name is ${this.name}`) + } + + const user = { + name: 'Tyler', + age: 27, + } + + greet.call(user) \ No newline at end of file From 7a9fab4438f4f5e8a0d5086ee8ec4345d46a2a01 Mon Sep 17 00:00:00 2001 From: reggieTheCoder <33520187+reggieTheCoder@users.noreply.github.com> Date: Thu, 10 Oct 2019 20:52:11 -0400 Subject: [PATCH 2/4] mvp complete --- assignments/prototypes.js | 54 ++++++++++++++++++++++++++++++--------- assignments/this.js | 8 +++--- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..c5d2c9ce2 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -7,7 +7,7 @@ Each constructor function has unique properties and methods that are defined in their block comments below: */ - + /* === GameObject === * createdAt @@ -16,6 +16,15 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(gamAttributes) { + this.createAt = gamAttributes.createdAt; + this.name = gamAttributes.name; + this.dimensions = gamAttributes.dimensions; +} +GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game`; +} + /* === CharacterStats === * healthPoints @@ -23,6 +32,15 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(charAttributes) { + this.healthPoints=charAttributes.healthPoints; + GameObject.call (this, charAttributes); +} +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 +50,28 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - + +function Humanoid(humanAttributes) { + this.team = humanAttributes.team; + this.weapons = humanAttributes.weapons; + this.langauge = humanAttributes.langauge; + CharacterStats.call (this, humanAttributes); +} + 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. -*/ + * 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,9 +132,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. -*/ - // 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! \ No newline at end of file diff --git a/assignments/this.js b/assignments/this.js index 9694d2df3..58b4609fe 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -55,12 +55,12 @@ function CordialPerson(greeter) { // code example for Explicit Binding function greet () { - alert(`Hello, my name is ${this.name}`) + alert(`Hello, my name is ${this.name} and I am ${this.age} years old.`) } const user = { - name: 'Tyler', - age: 27, + name: 'Reginald Alford', + age: 38, } - greet.call(user) \ No newline at end of file + greet.call(user); \ No newline at end of file From 51e329495223fc897696405f6fcae704b994db79 Mon Sep 17 00:00:00 2001 From: reggieTheCoder <33520187+reggieTheCoder@users.noreply.github.com> Date: Thu, 10 Oct 2019 20:55:23 -0400 Subject: [PATCH 3/4] mvp complete v2 --- assignments/prototypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index c5d2c9ce2..0a4385fad 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -54,7 +54,7 @@ CharacterStats.prototype.takeDamage= function() { function Humanoid(humanAttributes) { this.team = humanAttributes.team; this.weapons = humanAttributes.weapons; - this.langauge = humanAttributes.langauge; + this.language = humanAttributes.language; CharacterStats.call (this, humanAttributes); } Humanoid.prototype = Object.create(CharacterStats.prototype); From 891cd0087eba61627aebf8e47cdf74cae5365d1d Mon Sep 17 00:00:00 2001 From: reggieTheCoder <33520187+reggieTheCoder@users.noreply.github.com> Date: Fri, 11 Oct 2019 11:04:15 -0400 Subject: [PATCH 4/4] createdAt issue resolved --- assignments/prototypes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 0a4385fad..d3b4fac82 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -17,7 +17,7 @@ */ function GameObject(gamAttributes) { - this.createAt = gamAttributes.createdAt; + this.createdAt = gamAttributes.createdAt; this.name = gamAttributes.name; this.dimensions = gamAttributes.dimensions; }