From 45dd81abe927b6e33d604c73ae502167a8f7d886 Mon Sep 17 00:00:00 2001 From: John Nweke Date: Thu, 17 Oct 2019 15:26:36 -0400 Subject: [PATCH 1/3] Started Prototypes of GameObject and CharacterStats --- assignments/prototypes.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..dfbfec4c3 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -15,6 +15,18 @@ * 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 (attributes) { + this.createdAt = attributes.createdAt; + this.name = attributes.name; + this.dimensions = attributes.dimensions; +} + +GameObject.prototype.destroy = function () { + return `${this.name} was removed from the game.`; +}; + +//TEST +console.log(swordsman.destroy()); /* === CharacterStats === @@ -23,6 +35,17 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats (attributes) { + //Dot Call Method + GameObject.call(this, attributes); + this.healthPoints = attributes.healthPoints; +} +//Inheriting Game Object +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 @@ -102,7 +125,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. From d00fb6c7587d46d21ebbac8bf86a2ad118f9813d Mon Sep 17 00:00:00 2001 From: John Nweke Date: Fri, 18 Oct 2019 11:43:49 -0400 Subject: [PATCH 2/3] Completed Prototypes.js, no stretch yet --- assignments/prototypes.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index dfbfec4c3..aa7e87d52 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -26,7 +26,7 @@ GameObject.prototype.destroy = function () { }; //TEST -console.log(swordsman.destroy()); +//console.log(swordsman.destroy()); /* === CharacterStats === @@ -36,7 +36,7 @@ console.log(swordsman.destroy()); */ function CharacterStats (attributes) { - //Dot Call Method + //.Call Method GameObject.call(this, attributes); this.healthPoints = attributes.healthPoints; } @@ -55,7 +55,21 @@ CharacterStats.prototype.takeDamage = function () { * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - +function Humanoid (attributes) { + //.call Method + CharacterStats.call(this, attributes); + this.team = attributes.team; + this.weapons = attributes.weapons; + this.language = attributes.language; +} + +//Inheriting Character Object +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. @@ -64,7 +78,7 @@ CharacterStats.prototype.takeDamage = function () { // Test you work by un-commenting these 3 objects and the list of console logs below: -/* + const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -125,7 +139,7 @@ CharacterStats.prototype.takeDamage = function () { 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 c4efb1784341b26bd32e8e3c73dffdca730bc7a4 Mon Sep 17 00:00:00 2001 From: John Nweke Date: Fri, 18 Oct 2019 12:59:50 -0400 Subject: [PATCH 3/3] MVP + Stretch reached --- assignments/prototypes.js | 25 +++++++++++++++- assignments/this.js | 60 +++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index aa7e87d52..607a40f3e 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -70,8 +70,29 @@ Humanoid.prototype.greet = function () { return `${this.name} offers a greeting in ${this.language}.` }; +//VILLAIN CONSTRUCTOR (Stretch Task) +function Villain (attributes) { + // .call Method - calls the object you want to pull from + Humanoid.call(this, attributes); + this.goodOrEvil = attributes.goodOrEvil; +} + +//Inheriting Humanoid Object - references the current function and links to the function you want to pull from +Villain.prototype = Object.create(Humanoid.prototype); + +//HERO CONSTRUCTOR (Stretch Task) +function Hero (attributes) { + // .call Method + Humanoid.call (this, attributes); + this.goodOrEvil = attributes.goodOrEvil; +} + +//Inheriting Humanoid Object +Hero.prototype = Object.create(Humanoid.prototype); + + /* - * Inheritance chain: GameObject -> CharacterStats -> Humanoid + * Inheritance chain: GameObject -> CharacterStats -> Humanoid -> Villain & Hero * 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. */ @@ -93,6 +114,7 @@ Humanoid.prototype.greet = function () { 'Staff of Shamalama', ], language: 'Common Tongue', + }); const swordsman = new Humanoid({ @@ -110,6 +132,7 @@ Humanoid.prototype.greet = function () { 'Shield', ], language: 'Common Tongue', + goodOrEvil: 'Evil', }); const archer = new Humanoid({ diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..31299e598 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 Object Binding/Global Object Binding: In the global scope, the value of 'this' is the window object. +* 2. Implicit Binding: When a function is called using dot notation, the value of 'this' is the object to the left of the dot (or preceding the dot). +* 3. New Binding: When a constructor function is used (with the 'new' keyword), the value of 'this' is the specific instance of the object created by the constructor. +* 4. Explicit Binding: When JS .call or .apply methods are used, the value of 'this' is explicitly defined using .call, .apply, or .bind * * write out a code example of each explanation above */ @@ -13,14 +13,64 @@ // code example for Window Binding +function callMyPhone (number) { + console.log (this); //Outputs my entire Window Object! + return number; +} +//TEST +//callMyPhone(`NumberOne`); + // Principle 2 // code example for Implicit Binding +const typeOfPhone = { + phoneType: 'Samsung', + isIphone: false, + explain: function(phone) { + console.log(`John had a ${phone}, but now he uses a ${this.phoneType}.`); + console.log(this); + } +}; +//TEST +typeOfPhone.explain(`iPhone 5`); // Principle 3 // code example for New Binding +//Constructor Function +function Car (brand, model, year) { + this.brand = brand; + this.model = model; + this.year = year; + // this.print = function () { + // console.log(`John is getting a ${this.year} ${this.brand} ${this.model} before the end of the year.`); + // console.log(this); + // }; +} +//Make a Prototype function for Car +Car.prototype.print = function () { + console.log(`John is getting a ${this.year} ${this.brand} ${this.model} before the end of the year.`); + console.log(this); + }; + +//New Items from Constructor +const kia = new Car ('Kia', 'Niro', '2019'); +const jeep = new Car ('RangeRover', 'Sport', '2020'); +const mazda = new Car ('Mazda', 'CX-9', '2020'); + +//Call Constructor Method print +kia.print(); +jeep.print(); +mazda.print(); + + + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding + +//Overriding the New items from above using call and apply +kia.print.call(mazda); +jeep.print.apply(kia); +mazda.print.bind(jeep);