Skip to content
Merged
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
47 changes: 42 additions & 5 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
/*
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
* name
* 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;
}

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

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

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

// linking GameObject to CharacterStats so it can inherit the properties from GameObject.
CharacterStats.prototype=Object.create(GameObject.prototype);

//adding the takeDamage prototype function to CharacterStats:
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 +53,23 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/



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

// linking Humanoid to CharacterStats so Humanoid:
Humanoid.prototype=Object.create(CharacterStats.prototype);

//creating prototype function greet for humanoid:
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 +78,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 +139,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
23 changes: 19 additions & 4 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Default Binding: By default, this binds to the Window. If no reference is attached to this, it assumes it's bound to the current window and not anything beyond it. I.e. the window is the "forrest" where this resides by default.
* 2. Implicit Binding: When a method is called from an object, the object is referred to as "this". i like to think of this as a pronoun for the object I'm referencing.

* 3. Explicit Binding: This happens when .call(). .apply(), or .bind() are used on a function. It's called explicit due to us explicitly pass in a this context.
* 4. New Binding: This occurs when the new keyword is used to create an object via a constructor function. In this case, this refers to that specific instance of the object that is created by the constructor.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(this);
function windBind(test) {
console.log(this);
return test;
}
windBind("hello");

// Principle 2

// code example for Implicit Binding

const impBindObj= {
name:'Zelda',
sayMyName: function(){
console.log(`My name is ${this.name}`);
}
}

impBindObj.sayMyName()
// Principle 3

// code example for New Binding
Expand Down