diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..607a40f3e 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) { + //.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 @@ -32,16 +55,51 @@ * 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}.` +}; + +//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. */ // Test you work by un-commenting these 3 objects and the list of console logs below: -/* + const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -56,6 +114,7 @@ 'Staff of Shamalama', ], language: 'Common Tongue', + }); const swordsman = new Humanoid({ @@ -73,6 +132,7 @@ 'Shield', ], language: 'Common Tongue', + goodOrEvil: 'Evil', }); const archer = new Humanoid({ @@ -102,7 +162,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. 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);