From 58df6b0a39af99eabfd91e40507ad2723258fb70 Mon Sep 17 00:00:00 2001 From: toneiojimz Date: Wed, 16 Oct 2019 18:28:28 -0700 Subject: [PATCH] updated --- assignments/prototypes.js | 41 ++++++++++++++++++++-- assignments/this.js | 73 ++++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 10 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..996ee67a5 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 = Object.create(GameObject.prototype); + + GameObject.prototype.destroy= function(){ + return `${this.name} was removed from the game.`; + }; + + /* === CharacterStats === @@ -23,6 +35,18 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(stats){ + GameObject.call(this, stats) + this.healthPoints = stats.healthPoints + +} + +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,6 +56,19 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ + +function Humanoid(attrib){ + CharacterStats.call(this, attrib) + this.team = attrib.team, + this.weapons = attrib.weapons, + this.language =attrib.language +} + +Humanoid.prototype = Object.create(CharacterStats.prototype); + +Humanoid.prototype.greet = function(){ + return `${this.name} offers a greeting in ${this.language}` +} /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid @@ -41,7 +78,7 @@ // 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,7 +139,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..c1ff69086 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,83 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. Window binding: + +here "this" refers to the window or default context + +* 2. Implicit binding: + +it specifies to the left of "this" to which object it applies or refers to + +* 3. Explicit binding: + +telling an object what function to use, having call, apply and bind as options. every option has a different behavior + +* 4. New Binding: + +creating a new object and "this" is the relation between new object and existing objects + * * write out a code example of each explanation above */ // Principle 1 - // code example for Window Binding +const myFunction = function() { + console.log(this.f); + } + + var f = 2 ; + myFunction(); + + + -// Principle 2 +//Principle 2 // code example for Implicit Binding +let myCat = { + name: 'moles', + noise: 'purrrrrr', + cat: function(){ + console.log(this.noise); + } +} + +myCat.cat(); + -// Principle 3 + + +// Principle 3 // code example for New Binding +function Activity(movie){ + this.time = movie; +} + +let myActivity = new Activity('Joker'); + +console.log(`My next movie is the ${myActivity.time}`); + + + + + // Principle 4 +// code example for Explicit Binding + +function Cooking(){ + console.log(this.sauce); +} + +let food = { + dish: 'pasta', + sauce: 'alfredo!' +} + +letsCook = Cooking.bind(food); + -// code example for Explicit Binding \ No newline at end of file +letsCook();