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
240 changes: 165 additions & 75 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

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

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

/*
=== GameObject ===
* createdAt
Expand All @@ -32,79 +32,169 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

/*
* 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.
*/
function GameObject(gameObj) {
this.createdAt = gameObj.createdAt;
this.name = gameObj.name;
this.dimensions = gameObj.dimensions;
}

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

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

CharacterStats.prototype = Object.create(GameObject.prototype);

CharacterStats.prototype.takeDamage = function() {
return `${this.name} took damage.`;
};

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

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

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

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!

function Hero(heroObj) {
Humanoid.call(this, heroObj);
this.dmg = heroObj.dmg;
}

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

Hero.prototype.hit = function(enemy) {
if (enemy.healthPoints <= 0) {
return "Game Over! Stop hitting your enemy!!!";
}
enemy.healthPoints = enemy.healthPoints - this.dmg;
let leftHealthMessage = "";
if (enemy.healthPoints <= 0) {
enemy.healthPoints = 0;
leftHealthMessage = `Now ${enemy.name} is dead. ${this.name} wins the game.`;
} else {
leftHealthMessage = `Now ${enemy.name} has ${enemy.healthPoints} health left.`;
}
return `${this.name} hit ${this.dmg} damage to ${enemy.name}. ${leftHealthMessage}`;
};

function Villain(villainObj) {
Hero.call(this, villainObj);
}

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

const pudge = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4
},
healthPoints: 100,
name: "Pudge",
team: "Radiant",
weapons: ["Hook", "Dismember"],
language: "Butcherish",
dmg: 20
});

const sniper = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 2
},
healthPoints: 75,
name: "Sniper",
team: "Dire",
weapons: ["Rifle", "Sharapnel"],
language: "Sniperish",
dmg: 30
});

// 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!
console.log(sniper.hit(pudge));
console.log(sniper.hit(pudge));
console.log(pudge.hit(sniper));
console.log(pudge.hit(sniper));
console.log(sniper.hit(pudge));
console.log(sniper.hit(pudge));
console.log(sniper.hit(pudge));
81 changes: 66 additions & 15 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* 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 Binding: In global context, 'this' keyword becomes 'window' object of Javascript.
Window object is everything that is Javascript that is built in our browser.
* 2. Implicit Binding: When we call a function of an object, we call it as 'myObject.myFunction()'
In this context, 'this' refers to whatever object is on the left side of the dot.
* 3. New Binding: We create a constructor function that we use to create objects.
Then we create calling the constructor function with the 'new' keyword.
In this case, 'this' refers to the newly created object with the 'new' keyword.
* 4. Explicit Binding: We call our function but we pass a new context (an Object) into either
.call or .apply method. In this example, 'this' refers to whatever object we pass
into the .call or .apply method.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +19,59 @@

// code example for Window Binding

// Principle 2

// code example for Implicit Binding

// Principle 3

// code example for New Binding

// Principle 4

// code example for Explicit Binding
function whereAmI() {
console.log(`I am at ${this.location.host}.`);
console.log(`Today is ${this.Date()}.`);
}
whereAmI();
// Here 'this' keyword refers to the global object that is 'window'.

// Principle 2

// code example for Implicit Binding

const book = {
title: "Alchemist",
author: "Paulo Coelho",
speak: function() {
console.log(`${this.title} is one of the classics by ${this.author}`);
}
};

book.speak();
// In this example, this keyword in speak function refers to the book object.

// Principle 3

// code example for New Binding

function Book(object) {
this.title = object.title;
this.author = object.author;
this.speak = function() {
console.log(`${this.title} is one of the classics by ${this.author}`);
};
}

const theFortyRulesOfLove = new Book({
title: "The Forty Rules of Love",
author: "Elif Shafak"
});

theFortyRulesOfLove.speak();
// In this example, 'this' refers to the object we created with the new keyword.

// Principle 4

// code example for Explicit Binding

// I will use the last constructor function to create a new object.

const badass = new Book({ title: "You are a BADASS", author: "Jen Sincero" });

console.log(".apply example");
theFortyRulesOfLove.speak.apply(badass);
// Here, 'this' keyword refers to whatever is passed into the .apply method.
console.log(".call example");
badass.speak.call(theFortyRulesOfLove);
// Here, 'this' keyword refers to whatever is passed into the .call method.