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
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ 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
* [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
Expand All @@ -30,7 +30,7 @@ Having a solid understanding of how `this` works will give you a huge advantage

## 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/prototypes.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.
41 changes: 38 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,35 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

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

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(Stat) {
this.healthPoints = Stat.healthPoints;

GameObject.call(this, Stat);
}

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


function Humanoid(HumAttr) {
this.team = HumAttr.team;
this.weapons = HumAttr.weapons;
this.language = HumAttr.language;

CharacterStats.call(this, HumAttr);
}

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 +76,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,7 +137,7 @@
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.
Expand Down
89 changes: 81 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,99 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window / Global
* 2. Implicit
* 3. New
* 4. Explicit
*
* write out a code example of each explanation above
*/

// Principle 1

// Window / Global
// code example for Window Binding
function global(Window){
console.log(this);
return `This is a ${Window} binding`;
}

// Principle 2
console.log(global("Window / Global"));

// Principle 2
// Implicit
// code example for Implicit Binding
const lightYear = {
greet: 'Welcome',
intro: function(name){
console.log(`${this.greet} ${name}, to Infinity and Beyond!`)
console.log(this);
}
};

// Principle 3
lightYear.intro('Buzz');

// Principle 3
// New
// code example for New Binding
function acquire(weapon) {
this.weapons = weapon;
this.obtained = "You have obtained a ";
this.achieved = function() {
console.log(this.obtained + this.weapons + '!');
console.log(this);
}
}

const axe = new acquire('Axe');
const doubleAxe = new acquire('Double Axe');
const sword = new acquire('Sword');
const dagger = new acquire('Dagger');
const stave = new acquire('Stave');
const wand = new acquire('Wand');
const bow = new acquire('Bow');
const xbow = new acquire('Crossbow');

axe.achieved();
doubleAxe.achieved();
sword.achieved();
dagger.achieved();
stave.achieved();
wand.achieved();
bow.achieved();
xbow.achieved();

// Principle 4
// Explicit
// code example for Explicit Binding

const Mig = {
name: "Tiny",
feet: 3,
job: "Beta Tester",
update: function() {
return `${this.name} is best. (He likes to speak in third person.)`;
}
}

const pup = {
name: "Gir",
feet: 2,
job: "Destroy the world",
update: function() {
return `I am ${this.name}`;
}
}

const games = ['Call of Duty', 'Battlefield', 'Counter-Strike'];
const invader = ['"Aww, It likes me!"', '"I loveded you piggy, I loveded you"', '"Where are my walnuts?"'];

function present(game1, game2, game3) {
return `${this.name} is skilled in ${game1}, ${game2}, and ${game3}!`;
}

function invaderZim(phrase1, phrase2, phrase3) {
return `${this.name} best phrases are ${phrase1}, ${phrase2}, ${phrase3}!`
}

// code example for Explicit Binding
console.log(present.apply(Mig, games));
console.log(invaderZim.call(pup, ...invader));