Skip to content

Greg hayes #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 108 additions & 72 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@

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(attribute) {
this.createdAt = attribute.createdAt;
this.name = attribute.name;
this.dimensions = attribute.dimensions;

//******** The below function can be abstracted with a prototype ********/
// this.destroy = function() {
// return `${this.newName} was removed from the game.`;
//};
}

GameObject.prototype.destroy = function() {
// Protoype
return `${this.name} was removed from the game.`;
};

/*
=== CharacterStats ===
Expand All @@ -23,6 +39,18 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats(stats) {
GameObject.call(this, stats);
this.healthPoints = stats.healthPoints;
}

//Object.create() allows the constructor function to see/inherit a parent function
CharacterStats.prototype = Object.create(GameObject.prototype);

CharacterStats.prototype.takeDamage = function() {
// Protoype
return `${this.name} took damage.`;
};
/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,79 +60,87 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(profile) {
CharacterStats.call(this, profile);
this.team = profile.team;
this.weapons = profile.weapons;
this.language = profile.language;

// *** Again abstracting with a prototype below***//
// this.greet = function() {
// return `${this.newName} offers a greeting in ${this.newLanguage}.`;
// };
}

//Object.create() allows the constructor function to see/inherit a parent function
Humanoid.prototype = Object.create(CharacterStats.prototype);

Humanoid.prototype.greet = function() {
return `${this.newName} offers a greeting in ${this.newLanguage}.`;
};

/*
* 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.
*/

// 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!
// 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!
59 changes: 45 additions & 14 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
/* 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. this is applied to Window object by default is if has no other context.
* 2. this is applied to whatever object is left of the '.' when we invoke the function
* 3. when a function is invoked w/ the 'new' keyword, this points to the new function
* 4. You can use .call / .apply / .bind to tell this the function to work from
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function Person(attribute) {
// this.name = attribute.name;
console.log(this.says);
}
Person();

// Principle 2
const says = "Hello";

// Principle 2
// code example for Implicit Binding
const user1 = {
name: "Greg",
greet() {
return `Hi, my name is ${this.name}.`;
}
};
//this.name will use const user as its context
console.log(user1.greet());

// Principle 3

// code example for New Binding

const user2 = new Person({
location: "Austin, TX",
tell() {
return `I live in ${this.location}.`;
}
});
console.log(user2);
// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
function GameObject(attribute) {
this.createdAt = attribute.createdAt;
this.name = attribute.name;
this.dimensions = attribute.dimensions;
}

//Using .call this function now inherits from the GameObject constructor
function CharacterStats(stats) {
GameObject.call(this, stats);
this.healthPoints = stats.healthPoints;
}

console.log(CharacterStats());