From 1d53fbb2a74219b971ec663e32164e2e898dfddb Mon Sep 17 00:00:00 2001 From: Jessica Novak Date: Wed, 16 Oct 2019 15:40:25 -0700 Subject: [PATCH 1/2] initial commit --- assignments/prototypes.js | 198 ++++++++++++++++++++------------------ assignments/this.js | 48 +++++++-- 2 files changed, 144 insertions(+), 102 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..86b739df8 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -1,108 +1,122 @@ /* - Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy. - + Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance heirarchy. In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid. - At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions. - Each constructor function has unique properties and methods that are defined in their block comments below: -*/ - -/* - === GameObject === - * createdAt - * name - * dimensions (These represent the character's size in the video game) - * destroy() // prototype method that returns: `${this.name} was removed from the game.` + Each constructor function has unique properites and methods that are defined in their block comments below: */ -/* - === CharacterStats === - * healthPoints - * takeDamage() // prototype method -> returns the string ' took damage.' - * should inherit destroy() from GameObject's prototype -*/ -/* - === Humanoid (Having an appearance or character resembling that of a human.) === - * team - * weapons - * language - * greet() // prototype method -> returns the string ' offers a greeting in .' - * 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. -*/ +function GameObject(attributes) { + this.createdAt = attributes.createdAt; + this.dimensions = attributes.dimensions; +} +// prototype method -> returns the string: 'Object was removed from the game.' +GameObject.prototype.destroy = function () { + return (`${this.name} was removed from the game`) +} +// ========================================================================= +function CharacterStats(stats) { + this.hp = stats.hp; + this.name = stats.name; + // *should inherit destroy() from GameObject 's prototype + GameObject.call(this, stats); +} + +CharacterStats.prototype = Object.create(GameObject.prototype); -// Test you work by un-commenting these 3 objects and the list of console logs below: +const stats1 = new CharacterStats({ + hp: 5, + name: 'bob' +}) +// prototype method -> returns the string ' took damage.' +CharacterStats.prototype.takeDamage = function () { + return `${this.name} took damage` +} +// ============================================================================= +function Humanoid(stuff) { + this.faction = stuff.faction; + this.weapons = stuff.weapons; + this.language = stuff.language; + // * should inherit destroy() from GameObject through CharacterStats + // * should inherit takeDamage() from CharacterStats + CharacterStats.call(this, stuff); +} +Humanoid.prototype = Object.create(CharacterStats.prototype); +// prototype method -> returns the string ' offers a greeting in .' +Humanoid.prototype.greet = function () { + return (`${this.name} offers a greeting in ${this.language}`) +} /* - 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', - }); + * 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. + */ - 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', - }); +// Test you work by uncommenting these 3 objects and the list of console logs below: - 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', - }); +const mage = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + hp: 5, + name: 'Bruce', + faction: 'Mage Guild', + weapons: [ + 'Staff of Shamalama', + ], + language: 'Common Toungue', +}); + +const swordsman = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 2, + height: 2, + }, + hp: 15, + name: 'Sir Mustachio', + faction: 'The Round Table', + weapons: [ + 'Giant Sword', + 'Shield', + ], + language: 'Common Toungue', +}); + +const archer = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4, + }, + hp: 10, + name: 'Lilith', + faction: '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.hp); // 15 + console.log(mage.name); // Bruce + console.log(swordsman.faction); // 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. - 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. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..dc11de4b3 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,54 @@ /* 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. Window - works in global scope + * 2. Implicit - only communicates inside of the function/object that it lives in + * 3. New - works with constructor functions, constructors always have a capital letter + * 4. Explicit - 'this' is defined when the call or apply method is used + * + * write out a code example of each explanation above + */ // Principle 1 // code example for Window Binding +function myFunc(prop) { + console.log(this); + return prop; + } + myFunc("This is a window binding"); // Principle 2 // code example for Implicit Binding +const jessIMP = { + name: 'Jess', + color: 'orange', + food: 'thanksgiving dinner', + favoriteColor: function() { + console.log(`${this.name}'s favorite color is ${this.color}.`) + } + } + + jessIMP.favoriteColor(); // Principle 3 // code example for New Binding +function Person(name, color) { + this.name = name; + this.color = color; + } + + const bobby = new Person('bob', 'yellow'); + + console.log(bobby); // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +let favoriteFood = function() { + console.log(`${this.name}'s favorite food is ${this.food}.`) + } + + favoriteFood.call(jessIMP); \ No newline at end of file From d37aadb7ff5fb4887b19f13dc0ade929beb51754 Mon Sep 17 00:00:00 2001 From: Jessica Novak Date: Thu, 17 Oct 2019 15:14:06 -0700 Subject: [PATCH 2/2] final final commit --- assignments/prototypes.js | 221 +++++++++++++++++++++----------------- assignments/this.js | 36 ++++--- 2 files changed, 141 insertions(+), 116 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 86b739df8..14b8cc88b 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -1,121 +1,142 @@ /* - Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance heirarchy. + Object oriented design is commonly used in video games. + For this part of the assignment you will be implementing several constructor functions + with their correct inheritance hierarchy. In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid. - At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions. + At the bottom of this file are 3 objects that all end up inheriting from Humanoid. + Use the objects at the bottom of the page to test your constructor functions. - Each constructor function has unique properites and methods that are defined in their block comments below: + Each constructor function has unique properties and methods that are defined in their block comments below: +*/ +/* + === GameObject === + * createdAt + * name + * 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.dimensions = attributes.dimensions; +function GameObject(attr){ + this.createdAt = attr.createdAt; + this.name = attr.name; + this.dimensions = attr.dimensions; } -// prototype method -> returns the string: 'Object was removed from the game.' -GameObject.prototype.destroy = function () { - return (`${this.name} was removed from the game`) + +GameObject.prototype.destroy = function(){ + return `${this.name} was removed from the game.` } -// ========================================================================= -function CharacterStats(stats) { - this.hp = stats.hp; - this.name = stats.name; - // *should inherit destroy() from GameObject 's prototype + +/* + === CharacterStats === + * healthPoints + * takeDamage() // prototype method -> returns the string ' took damage.' + * should inherit destroy() from GameObject's prototype +*/ + +function CharacterStats(stats){ GameObject.call(this, stats); + this.healthPoints = stats.healthPoints; + this.name = stats.name; } CharacterStats.prototype = Object.create(GameObject.prototype); -const stats1 = new CharacterStats({ - hp: 5, - name: 'bob' -}) -// prototype method -> returns the string ' took damage.' -CharacterStats.prototype.takeDamage = function () { - return `${this.name} took damage` +CharacterStats.prototype.takeDamage = function(){ + return `${this.name} took damage.`; } -// ============================================================================= -function Humanoid(stuff) { - this.faction = stuff.faction; - this.weapons = stuff.weapons; - this.language = stuff.language; - // * should inherit destroy() from GameObject through CharacterStats - // * should inherit takeDamage() from CharacterStats - CharacterStats.call(this, stuff); + +/* + === Humanoid (Having an appearance or character resembling that of a human.) === + * team + * weapons + * language + * greet() // prototype method -> returns the string ' offers a greeting in .' + * should inherit destroy() from GameObject through CharacterStats + * should inherit takeDamage() from CharacterStats +*/ + +function Humanoid(about){ + CharacterStats.call(this, about); + this.team = about.team; + this.weapons = about.weapons; + this.language = about.language; } + Humanoid.prototype = Object.create(CharacterStats.prototype); -// prototype method -> returns the string ' offers a greeting in .' -Humanoid.prototype.greet = function () { - return (`${this.name} offers a greeting in ${this.language}`) +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. - * Instances of CharacterStats should have all of the same properties as GameObject. - */ - -// Test you work by uncommenting these 3 objects and the list of console logs below: - -const mage = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 1, - height: 1, - }, - hp: 5, - name: 'Bruce', - faction: 'Mage Guild', - weapons: [ - 'Staff of Shamalama', - ], - language: 'Common Toungue', -}); - -const swordsman = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 2, - height: 2, - }, - hp: 15, - name: 'Sir Mustachio', - faction: 'The Round Table', - weapons: [ - 'Giant Sword', - 'Shield', - ], - language: 'Common Toungue', -}); - -const archer = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 1, - width: 2, - height: 4, - }, - hp: 10, - name: 'Lilith', - faction: '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.hp); // 15 - console.log(mage.name); // Bruce - console.log(swordsman.faction); // 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. + * 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. +*/ + +// 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. // Stretch task: diff --git a/assignments/this.js b/assignments/this.js index dc11de4b3..09eec03c1 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,36 +1,40 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * - * 1. Window - works in global scope - * 2. Implicit - only communicates inside of the function/object that it lives in - * 3. New - works with constructor functions, constructors always have a capital letter - * 4. Explicit - 'this' is defined when the call or apply method is used + * 1. Window - works in global scope, "this" refers to window or console object. + + * 2. Implicit - only communicates inside of the function/object that it lives in. + * "this" points to what comes to the left of the dot. + + * 3. New - works with constructor functions, constructors always have a capital letter. + "this" refers to the new object created. + + * 4. Explicit - "this" is defined when the call or apply method is used. * * write out a code example of each explanation above */ -// Principle 1 + + // Principle 1 // code example for Window Binding -function myFunc(prop) { - console.log(this); - return prop; - } - myFunc("This is a window binding"); + + console.log(this); //"this" refers to the global scope + // Principle 2 // code example for Implicit Binding -const jessIMP = { +const jessNOV = { name: 'Jess', color: 'orange', - food: 'thanksgiving dinner', + food: 'Thanksgiving dinner', favoriteColor: function() { console.log(`${this.name}'s favorite color is ${this.color}.`) } } - jessIMP.favoriteColor(); + jessNOV.favoriteColor(); //"this" refers to name and color (left of the dots) // Principle 3 @@ -40,9 +44,9 @@ function Person(name, color) { this.color = color; } - const bobby = new Person('bob', 'yellow'); + const jane = new Person('jane doe', 'yellow'); - console.log(bobby); + console.log(jane); //"this" refers to Person // Principle 4 @@ -51,4 +55,4 @@ let favoriteFood = function() { console.log(`${this.name}'s favorite food is ${this.food}.`) } - favoriteFood.call(jessIMP); \ No newline at end of file + favoriteFood.call(jessNOV); //"this" refers to jessNOV \ No newline at end of file