Skip to content
Closed
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
132 changes: 120 additions & 12 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
/*
Object oriented design is commonly used in video games. For this part of the assignment
you will be implementing several classes with their correct inheritance heirarchy.
you will be implementing several prototypes with their correct inheritance heirarchy.

In this file you will be creating three classes: GameObject, CharacterStats, Humanoid.
At the bottom of this file are 3 objects that all inherit from Humanoid. Use the objects at the bottom of the page to test your classes.
Each class has unique properites and methods that are defined in their block comments below:
In this file you will be creating three prototypes: GameObject, CharacterStats, Humanoid.
At the bottom of this file are 3 objects that all inherit from Humanoid. Use the objects at the bottom of the page to test your prototypes.

Each prototype has unique properites and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
* dimensions
* destroy() // prototype method -> returns the string 'Object was removed from the game.'
*/

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

/*
=== CharacterStats ===
* hp
Expand All @@ -23,6 +31,26 @@
* should inherit destroy() from GameObject's prototype
*/

const CharacterStats = function(parameter) {
this.hp = parameter.hp;
this.name = parameter.name;
GameObject.call(this, parameter);
this.takeDamage = function() {
return `${this.name} took damage.`;
}
this.harm = function() {
this.hp -= 5;
}
this.status = function() {
if (this.hp > 0) return `${this.name} has ${this.hp} hit points left.`;
else return `${this.name} is dead!`;
}
}

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



/*
=== Humanoid ===
* faction
Expand All @@ -32,7 +60,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


const Humanoid = function(parameter) {
this.faction = parameter.faction;
this.weapons = parameter.weapons;
this.language = parameter.language;
CharacterStats.call(this, parameter);
this.greet = function() {
return `${this.name} offers a greeting in ${this.language}.`;
}

}

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

/*
* Inheritance chain: Humanoid -> CharacterStats -> GameObject
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +82,7 @@

//Test you work by uncommenting these 3 objects and the list of console logs below:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,9 +143,76 @@
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 Villian and Hero classes that inherit from the Humanoid class.

// Stretch task:
// * Create Villian and Hero prototypes that inherit from the Humanoid prototype.
// * Give the Hero and Villians 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 villian and one a hero and fight it out with methods!
// * Create two new objects, one a villian and one a hero and fight it out with methods!

const Hero = function(parameter) {
Humanoid.call(this, parameter);
this.smite = function(target) {
this.harm.call(target);
return `${this.name} rightously smites ${target.name} for 5 damage!`;
}
}
Hero.prototype = Object.create(Humanoid.prototype);


const Villain = function(parameter) {
Humanoid.call(this, parameter);
this.bash = function(target) {
this.harm.call(target);
return `${this.name} brutally bashes ${target.name} for 5 damage!`;
}
}
Villain.prototype = Object.create(Humanoid.prototype);

const Mr_Hero = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
hp: 15,
name: 'Mister Heroic',
faction: 'The Good Guys',
weapons: [
'Shiny Sword',
],
language: 'Common Toungue',
});

const Mr_Villain = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
hp: 20,
name: 'Mister Villainous',
faction: 'The Bad Guys',
weapons: [
'Demonic Sword',
],
language: 'Orcish',
});


console.log(Mr_Villain.status());
console.log(Mr_Hero.status());
console.log(Mr_Hero.smite(Mr_Villain));
console.log(Mr_Villain.status());
console.log(Mr_Villain.bash(Mr_Hero));
console.log(Mr_Hero.status());
console.log(Mr_Hero.smite(Mr_Villain));
console.log(Mr_Villain.status());
console.log(Mr_Villain.bash(Mr_Hero));
console.log(Mr_Hero.status());
console.log(Mr_Hero.smite(Mr_Villain));
console.log(Mr_Villain.status());
console.log(Mr_Villain.bash(Mr_Hero));
console.log(Mr_Hero.status());
57 changes: 52 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**/
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window binding: the this keyword gets bound to the window object by default when misused.
* 2. This automatically binds to the object left of the . in the invocation if not told where to go.
* 3. Explicit binding binds this manually using call bind or apply.
* 4. New binding creates a new object and assigns this to that.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +14,60 @@

// code example for Window Binding

//const myObject = () => this.name = "Billy";
//myObject();
//console.log(window.name); //stoopid quokka doesn't have a window object

// Principle 2

// code example for Implicit Binding


const myImplicitObject = {
name: "Mister Object",
color: "red",
number: 11,

implicitMethod: function() {
return `I'm an object, my name is ${this.name}, my favorite color is ${this.color}, and my lucky number is ${this.number}.`;
}
}



console.log(myImplicitObject.implicitMethod());

// Principle 3

// code example for New Binding

const Person = function(hair_color, eye_color, height) {
this.hair_color = hair_color;
this.eye_color = eye_color;
this.height = height;
this.introduce = function() {
return `Hi, I have no name, but my hair is ${this.hair_color}, my eye color is ${this.eye_color}, and I am ${this.height} feet tall.`;
}
}

let someguy = new Person("brown", "green", 5);
console.log(someguy.introduce());
// Principle 4

// code example for Explicit Binding
const animal = {
name: "Mr. Fluffy",
species: "dog",
hair_length: "long",
woof: function() {
return "woof woof woof!";
}
}

const haircut = function() {
this.hair_length = "short";
}

haircut.call(animal);
console.log(animal.hair_length);

// code example for Explicit Binding