From 60ea0c94aed232e1916611a86f45d2b49d368454 Mon Sep 17 00:00:00 2001 From: Sara De La Cruz Date: Wed, 18 Sep 2019 14:14:48 -0500 Subject: [PATCH 1/3] this.js complete --- assignments/this.js | 60 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..b3c204bd5 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,60 @@ /* The for principles of "this"; -* in your own words. explain the four principle for the "this" keyword below. -* -* 1. -* 2. -* 3. -* 4. -* -* write out a code example of each explanation above -*/ + * in your own words. explain the four principle for the "this" keyword below. + * + * 1. When "This" is on the global scope, the value of the function will be the window object. *Unless using 'Strict' mode. + * 2. "This" is the object being called in a function whenever it's preceded by a dot. + * 3. In a constructor "This" refers to the specific instance of the object that is created and returned by the constructor function. + * 4. If 'call or apply' method is used, this is explicitly defined. + * + * write out a code example of each explanation above + */ // Principle 1 - // code example for Window Binding +// "use strict"; -// Principle 2 +// function strictExample() { +// console.log(this.example); +// } +// strictExample(); +// const example = "x"; + +// strictExample(); +// Principle 2 // code example for Implicit Binding -// Principle 3 +// let game = { +// name: "NinjaAssassin", +// players: 4, +// gamerTag: function() { +// console.log(this.name); +// } +// }; + +// game.gamerTag(); +// Principle 3 // code example for New Binding +// function Food(favorite) { +// this.cusine = favorite; +// } + +// let myFavouriteFood = new Food("Tacos"); + +// console.log(`My favourite food is ${myFavouriteFood.cusine}`); + // Principle 4 +// code example for Explicit Binding + +// function book() { +// console.log(this.name); +// } + +// let myBook = { +// name: "Harry Potter", +// page: 35 +// }; -// code example for Explicit Binding \ No newline at end of file +// book.call(myBook); From 172e5e93e25582cb52e2dd508388c353970a6133 Mon Sep 17 00:00:00 2001 From: Sara De La Cruz Date: Wed, 18 Sep 2019 18:02:22 -0500 Subject: [PATCH 2/3] prototype.js complete --- assignments/prototypes.js | 217 +++++++++++++++++++++++++------------- 1 file changed, 145 insertions(+), 72 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..ac92ced68 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 @@ -32,79 +32,152 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - + /* - * 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. + */ + +function GameObject(attributes) { + this.createdAt = attributes.createdAt, + this.name = attributes.name, + this.dimensions = attributes.dimensions +} + + +function CharacterStats(attributes) { + GameObject.call(this, attributes), + this.healthPoints = attributes.healthPoints +} + + +function Humanoid(attributes) { + CharacterStats.call(this, attributes), + this.team = attributes.team, + this.weapons = attributes.weapons, + this.language = attributes.language +} + + + + + +GameObject.prototype.destroy = function () { + return `${this.name} was removed from the game`; +} + +CharacterStats.prototype = Object.create(GameObject.prototype); + +CharacterStats.prototype.takeDamage = function () { + return `${this.name} took damage.`; +} + +Humanoid.prototype = Object.create(CharacterStats.prototype); + +Humanoid.prototype.greet = function () { + return `${this.name} offers a greeting in ${this.language}`; +} + + // Test you work by un-commenting these 3 objects and the list of console logs below: -/* - const mage = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 1, - height: 1, - }, - healthPoints: 5, - name: 'Bruce', - team: 'Mage Guild', - weapons: [ - 'Staff of Shamalama', - ], - language: 'Common Tongue', - }); - - const swordsman = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 2, - height: 2, - }, - healthPoints: 15, - name: 'Sir Mustachio', - team: 'The Round Table', - weapons: [ - 'Giant Sword', - 'Shield', - ], - language: 'Common Tongue', - }); - - const archer = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 1, - width: 2, - height: 4, - }, - healthPoints: 10, - name: 'Lilith', - team: 'Forest Kingdom', - weapons: [ - 'Bow', - 'Dagger', - ], - language: 'Elvish', - }); - - console.log(mage.createdAt); // Today's date - console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } - console.log(swordsman.healthPoints); // 15 - console.log(mage.name); // Bruce - console.log(swordsman.team); // The Round Table - console.log(mage.weapons); // Staff of Shamalama - console.log(archer.language); // Elvish - 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. -*/ +const mage = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + healthPoints: 5, + name: 'Bruce', + team: 'Mage Guild', + weapons: [ + 'Staff of Shamalama', + ], + language: 'Common Tongue', +}); +const swordsman = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 2, + height: 2, + }, + healthPoints: 15, + name: 'Sir Mustachio', + team: 'The Round Table', + weapons: [ + 'Giant Sword', + 'Shield', + ], + language: 'Common Tongue', +}); +const archer = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4, + }, + healthPoints: 10, + name: 'Lilith', + team: 'Forest Kingdom', + weapons: [ + 'Bow', + 'Dagger', + ], + language: 'Elvish', +}); + + +console.log(mage.createdAt); // Today's date +console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } +console.log(swordsman.healthPoints); // 15 +console.log(mage.name); // Bruce +console.log(swordsman.team); // The Round Table +console.log(mage.weapons); // Staff of Shamalama +console.log(archer.language); // Elvish +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! + + +const villain = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 5, + width: 6, + height: 10, + }, + healthPoints: 20, + name: 'Tom', + team: 'Mage Guild', + weapons: [ + 'Sword of Valhalla', + ], + language: 'Elvish', +}); +const hero = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 7, + width: 4, + height: 7, + }, + healthPoints: 21, + name: 'Jerry', + team: 'Hero Guild', + weapons: [ + 'Sword of Atlantis', + ], + language: 'Common Tongue', +}); - // 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 +console.log(villain.healthPoints); // Today's date +console.log(hero.weapons); \ No newline at end of file From fc1429b216c879ad552f214ed1f174c7b993c38c Mon Sep 17 00:00:00 2001 From: Sara De La Cruz Date: Wed, 18 Sep 2019 18:50:06 -0500 Subject: [PATCH 3/3] JavaScript-II completed --- assignments/this.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/assignments/this.js b/assignments/this.js index b3c204bd5..1398fd072 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -11,15 +11,15 @@ // Principle 1 // code example for Window Binding -// "use strict"; +"use strict"; -// function strictExample() { -// console.log(this.example); -// } -// strictExample(); -// const example = "x"; +function strictExample() { + console.log(this.example); +} +strictExample(); +const example = "x"; -// strictExample(); +strictExample(); // Principle 2 // code example for Implicit Binding