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

**Follow these steps to set up and work on your project:**

* [ ] Create a forked copy of this project.
* [ ] Add your project manager as collaborator on Github.
* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!).
* [ ] Create a new branch: git checkout -b `<firstName-lastName>`.
* [ ] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
* [ ] Push commits: git push origin `<firstName-lastName>`.
- [x] Create a forked copy of this project.
- [x] Add your project manager as collaborator on Github.
- [x] Clone your OWN version of the repository (Not Lambda's by mistake!).
- [x] Create a new branch: git checkout -b `<firstName-lastName>`.
- [x] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
- [x] Push commits: git push origin `<firstName-lastName>`.

**Follow these steps for completing your project.**

* [ ] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
* [ ] Add your project manager as a reviewer on the pull-request
* [ ] Your project manager will count the project as complete by merging the branch back into master.
- [x] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
- [x] Add your project manager as a reviewer on the pull-request
- [] Your project manager will count the project as complete by merging the branch back into master.

## Assignment Set Up

* Complete all the exercises as described inside each assignment file.
* To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console.
- Complete all the exercises as described inside each assignment file.
- To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console.

## The `this` keyword

Having a solid understanding of how `this` works will give you a huge advantage when you start building with more advanced frameworks. Use the [this.js](assignments/this.js) file to traverse through a few `this` problems.

## Prototype

The prototype challenge will focus on building prototypes for a fantasy themed game that includes mages, swordsmen, and archers. Follow the [prototypes.js](assignments/this.js) instructions closely to create the beginnings of what could be an awesome JavaScript game.
The prototype challenge will focus on building prototypes for a fantasy themed game that includes mages, swordsmen, and archers. Follow the [prototypes.js](assignments/this.js) instructions closely to create the beginnings of what could be an awesome JavaScript game.

* Read the instructions found within the file carefully to finish the challenges.
* Remember to un-comment the objects and console logs to test your work at the bottom of the page.
- Read the instructions found within the file carefully to finish the challenges.
- Remember to un-comment the objects and console logs to test your work at the bottom of the page.
170 changes: 98 additions & 72 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Each constructor function has unique properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +16,33 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(createdAt, name, dimensions) {
this.createdAt = createdAt;
this.dimensions = dimensions;
this.name = name;
}

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

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/

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

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,79 +52,85 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attrs) {
CharacterStats.call(
this,
attrs.createdAt,
attrs.name,
attrs.dimensions,
attrs.healthPoints
);
this.language = attrs.language;
this.team = attrs.team;
this.weapons = attrs.weapons;
}

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.
* 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!
50 changes: 38 additions & 12 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
/* The four principles of "this";
* in your own words. explain the four principles for the "this" keyword below.

* 1. Window binding -- The default binding. When variables are declared in the global scope, `this` will refer to the window object or
global process object in other host environments.
* 2. Implicit binding -- `this` refers to the parent object containing the `this` keyword as seen in object declarations.
* 3. Explicit binding - occurs the value of `this` is explicitly set via `call`, `bind` or `apply` functions.
* 4. New Binding - when creating a new object via a constructor function, `this` refers to the newly created object.

* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
var username = "austin";
console.log(this.username); // will output austin because username is bound to the window object.

// Principle 2

// code example for Implicit Binding
let player = {
name: "Magnus Carlsen",
title: "World Chess Champion",
introduceSelf: function() {
return `Hi, I'm ${this.name} and I am the ${this.title}`;
}
};

console.log(player.introduceSelf()); // Hi, I'm Magnus Carlsen and I am the World Chess Champion.

// Principle 3
// code example for Explicit Binding
function logMessage() {
console.log(`${this.name} is a great player!`);
}

// code example for New Binding
let players = [
{ name: "Lebron James" },
{ name: "Chris Paul" },
{ name: "Yao Ming" }
];

players.forEach(player => logMessage.call(player)); // loops through the array and logs message

// Principle 4
// code example for New Binding
function Language(name, type) {
this.name = name;
this.type = type;
}

// code example for Explicit Binding
let js = new Language("JavaScript", "Functional");
console.log(js.name, js.type); // JavaScript Functional