From 817adebeec4975a865ee1a3fb1198d0572b44477 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 10 Oct 2019 22:42:25 -0600 Subject: [PATCH] finished project --- assignments/prototypes.js | 38 +++++++++++++++++++++++++++-- assignments/this.js | 51 +++++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..e55431a8b 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -41,7 +41,41 @@ // Test you work by un-commenting these 3 objects and the list of console logs below: -/* +function GameObject(attrs) { + this.createdAt = attrs.createdAt; + this.name = attrs.name; + this.dimensions = attrs.dimensions; +}; + +GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game.` +}; + +CharacterStats.prototype = Object.create(GameObject.prototype); + +function CharacterStats(statsAttrs) { + GameObject.call(this, statsAttrs); + this.healthPoints = statsAttrs.healthPoints; +}; + +CharacterStats.prototype.takeDamage = function() { + return `${this.name} took damage.` +}; + +Humanoid.prototype = Object.create(CharacterStats.prototype); + +function Humanoid(humanAttrs) { + GameObject.call(this, humanAttrs); + CharacterStats.call(this, humanAttrs); + this.team = humanAttrs.team; + this.weapons = humanAttrs.weapons; + this.language = humanAttrs.language; +}; + +Humanoid.prototype.greet = function() { + return `${this.name} offers a greeting in ${this.language}`; +}; + const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -102,7 +136,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..9073cdb65 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. Global Binding - the value of "this" is the window or console. +* 2. Implicit Binding - "this" refers to the object that the method is acting on +* 3. New Binding - "this" refers to the new object created by the constructor function +* 4. Explicit Binding - whenever call or apply is used "this" is explicitly bound * * write out a code example of each explanation above */ @@ -13,14 +13,55 @@ // code example for Window Binding +function logHello(phrase) { + console.log(this); + return phrase; +}; +logHello('Hello'); + // Principle 2 // code example for Implicit Binding +let myObj = { + age: 27, + stateAge: function(name) { + console.log(`${name} is ${this.age}`); + console.log(this); + } +}; + +myObj.stateAge("Jack"); + // Principle 3 // code example for New Binding +function stateAge(person) { + this.age = 27, + this.person = person, + this.state = function () { + return `${this.person} is ${this.age}`; + console.log (this); + }; +} + +const jack = new stateAge('Jack'); +jack.state(); + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding + +function stateAge(person) { + this.age = 27, + this.person = person, + this.state = function () { + return `${this.person} is ${this.age}`; + console.log (this); + }; +} + +const jim = new stateAge('Jack'); +const jill = new stateAge('Jill'); +jim.state.call(jill); \ No newline at end of file