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,18 +6,18 @@ 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
* [x] Your project manager will count the project as complete by merging the branch back into master.

## Assignment Set Up

Expand Down
77 changes: 73 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,30 @@
* 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(obj) {
this.createdAt = obj.createdAt;
this.name = obj.name;
this.dimensions = obj.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(obj) {
this.healthPoints = obj.healthPoints;
GameObject.call(this, obj);
}
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.) ===
Expand All @@ -32,6 +49,16 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(obj) {
this.team = obj.team;
this.weapons = obj.weapons;
this.language = obj.language;
CharacterStats.call(this, obj);
}
Humanoid.prototype = Object.create(CharacterStats.prototype);
Humanoid.prototype.greet = function() {
return `${this.name} offers a greeting in ${this.language}`;
}

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +68,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 +129,51 @@
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.
// * 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!
// * Create two new objects, one a villain and one a hero and fight it out with methods!

function Villain(obj) {
Humanoid.call(this, obj);
}
Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.injure = function(obj) {
if (obj.healthPoints === 1) {
obj.destroy();
} else {
obj.healthPoints -= 1;
}
}

function Hero(obj) {
Humanoid.call(this, obj);
}
Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.save = function(obj) {
obj.healthPoints += 1;
}

const villian = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Villian',
team: 'Dragons',
weapons: [
'Fire'
],
language: 'telepathy',
})

console.log(archer);
villian.injure(archer)
console.log(archer)
villian.injure(archer)
console.log(archer)
31 changes: 26 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
/* 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 object binding to 'this' happens when the function gets called from the global scope and has no other bindings
applied to it.
* 2. Implicit Binding to 'this' happens when the function is a part of the object containig other properties to which in this case
functions 'this' will be referrring.
* 3. New Binding to 'this' happens when we call the constructor function with the keyword 'new'; 'this' will refer to properties of the constructor function.
* 4. Explicit Binding to 'this' happens when we explicitely indicate what we want 'this' to refer to by using .call and .apply.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
let obj = {
'name': 'Kitler',
'foo': function() {
console.log(this.name);
}
}
let name = "Rosie";
let func = obj.foo;
console.log(func()); //even though obj has its own name property, the binding to 'this' falls into global scope

// Principle 2

// code example for Implicit Binding
console.log(obj.foo()) //since the function is being called as a part of the 'obj', 'this' binds to the 'obj's property 'name'.

// Principle 3

// code example for New Binding
function ConstructorFunc(obj) {
this.name = obj;
}
let bob = new ConstructorFunc('Bob')
console.log(bob.name)

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
let obj2 = {
'name': 'Mary'
}
console.log(obj.foo.call(obj2)); //we explicitely indicate what we want 'this' to refer to; in this case, it's obj2's property 'name'.