Skip to content
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ This challenge focuses on using the `this` keyword as well as getting comfortabl

## Git Set up

* [ ] Fork the project into your GitHub user account
* [ ] Clone the forked project into a directory on your machine
* [ ] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
* [ ] You are now ready to build this project with your preferred IDE
* [x] Fork the project into your GitHub user account
* [x] Clone the forked project into a directory on your machine
* [x] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
* [x] You are now ready to build this project with your preferred IDE

## Assignment Set Up

Expand Down
142 changes: 135 additions & 7 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*
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 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 properties and methods that are defined in their block comments below:
*/
Expand All @@ -15,6 +18,15 @@
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/

function GameObject(GameObjectAttrs) {
this.createdAt = Date();
this.dimensions = GameObjectAttrs.dimensions;
}

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

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

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

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
Expand All @@ -32,7 +56,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


function Humanoid(humanoidAttrs) {
CharacterStats.call(this, humanoidAttrs);
this.team = humanoidAttrs.team;
this.weapons = humanoidAttrs.weapons;
this.language = humanoidAttrs.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
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -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: {
Expand Down Expand Up @@ -102,9 +139,100 @@
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!
// * 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!

// Hero constructor

function Hero(heroAttrs) {
Humanoid.call(this, heroAttrs);
}

Hero.prototype = Object.create(Humanoid.prototype);

Hero.prototype.heroicDeed = function(target) {
if ( target.healthPoints > 0) {
target.healthPoints += -1;
} else {
return `This person is already dead!`;
};
if (target.healthPoints > 0) {
return `${this.name} has wounded ${target.name} for 1hp! ${target.name} has ${target.healthPoints} remaining!`;
} else if (target.healthPoints === 0) {
return `${this.name} has slain ${target.name}!`;
};
}

// Villain constructor

function Villain(villainAttrs) {
Humanoid.call(this, villainAttrs);
}

Villain.prototype = Object.create(Humanoid.prototype);

Villain.prototype.villainousAct = function(target) {
if ( target.healthPoints > 0) {
target.healthPoints += -1;
} else {
return `This person is already dead!`;
};
if (target.healthPoints > 0) {
return `${this.name} has wounded ${target.name} for 1hp! ${target.name} has ${target.healthPoints} remaining!`;
} else if (target.healthPoints === 0) {
return `${this.name} has slain ${target.name}!`;
};
}

// Hero and Villain examples


const protagonist = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 5,
name: 'Sir Tain',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const antagonist = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 4,
name: 'Sir Lee',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

console.log(antagonist.greet());
console.log(protagonist.greet());
console.log(antagonist.villainousAct(protagonist));
console.log(protagonist.heroicDeed(antagonist));
console.log(antagonist.villainousAct(protagonist));
console.log(protagonist.heroicDeed(antagonist));
console.log(antagonist.villainousAct(protagonist));
console.log(protagonist.heroicDeed(antagonist));
console.log(antagonist.villainousAct(protagonist));
console.log(protagonist.heroicDeed(antagonist));
48 changes: 43 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -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. By default, "this" binds to the Window. That means the value of "this" will be the Window Object.
* 2. Implicit binding means "this" will be whatever called it. So, with example.this, "this" becomes "example".
* 3. When using a constructor function, "this" becomes the name of the function created.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 3. When using a constructor function, "this" becomes the name of the function created.
* 3. When using a constructor function, "this" becomes the name of the object created.

constructor functions return a new object

* 4. When using call or apply, "this" becomes the name of whatever is called/applied. e.g. If you reference a variable, the variale name becomes "this".

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using .call / .apply on a function you can bind this to an object also

*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,52 @@

// code example for Window Binding

console.log(this);

// Principle 2

// code example for Implicit Binding

const anObj = {
first: "1st",
second: "2nd",
aFunc: function() {
console.log(`The value of this object's keys are ${this.first} and ${this.second}.`);
},
}

// Principle 3

// code example for New Binding

function ExampleObj(example) {
this.key = example;
console.log(example);
this.reveal = function() {
return this.key;
};
}

const firstExample = new ExampleObj('first example'); //?
const secondExample = new ExampleObj('second example');

firstExample.reveal(); //?
secondExample.reveal(); //?

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

function ExplicitObj(example) {
this.key = example;
console.log(example);
this.reveal = function() {
return this.key;
};
}

const firstExplicit = new ExplicitObj('first example'); //?
const secondExplicit = new ExplicitObj('second example');

firstExplicit.reveal.call(secondExplicit); //?
secondExplicit.reveal.apply(firstExplicit); //?