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
84 changes: 75 additions & 9 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.

In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.
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 @@ -16,13 +16,36 @@
* 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(charAttr) {
this.healthPoints = charAttr.healthPoints;
GameObject.call(this, charAttr);

};

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 +55,22 @@
* 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 +79,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 +140,37 @@
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.

// 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!
// * Create two new objects, one a villain and one a hero and fight it out with methods!

// Villain
function Villian(vilAttr) {
this.isVillain = vilAttr.isVillain;
Humanoid.call(this, vilAttr);
};

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

Villian.prototype.checkIfVillian = function() {
if(this.isVillian) {
return 'I\'m the villian of this story!';
}
};

// Hero
function Hero(herAttr) {
this.isHero = herAttr.isHero;
Humanoid.call(this, herAttr);
};

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

Hero.prototype.checkIfHero = function() {
if(this.isHero) {
return 'I\'m the brave hero of this story!';
}
};
45 changes: 40 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* Principle 1: Window - Whenever a function is within the global scope, the value of 'this' will be the global 'window' object. Synomynous with using "window." before calling a function.
* Principle 2: Implicit Binding - Whenever a function is preceeded by a dot and called, the object before that dot is what 'this' points to.
* Principle 3: New Binding - When using the 'new' keyword, 'this' refers to the specific instance of an object created and returned using a constructor function.
* Principle 4: Explicit Binding - 'Call' or 'Apply' methods explicitly define 'this'.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function food(food, type) {
console.log(food + ' is a ' + type);
console.log(this);
}

food('Potato', 'vegetable');

// Principle 2

// code example for Implicit Binding

let foodTypes = {
middle: ' is a ',
speak: function(food, type) {
console.log(food + this.middle + type);
console.log(this);
}
}

foodTypes.speak('Pineapple', 'fruit');

// Principle 3

// code example for New Binding

function TypeOfFood(food, type) {
this.middle = ' is a ';
this.food = food;
this.type = type;
this.speak = function() {
console.log(this.food + this.middle + this.type);
console.log(this);
}
}

var mango = new TypeOfFood('Mango', 'fruit');
var celery = new TypeOfFood('Celery', 'vegetable');

mango.speak();
celery.speak();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

mango.speak.call(celery);
celery.speak.apply(mango);