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
50 changes: 41 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,32 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(attrs) {
this.createdAt = attrs.createdAt;
this.name = attrs.name;
this.dimensions = attrs.dimensions;
this.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 (attrs) {
GameObject.call(this, attrs);
this.healthPoints = attrs.healthPoints;
this.name = attrs.name;
this.takeDamage = function () {
return `${this.name} took damage.`
}
}

CharacterStats.prototype = Object.create(GameObject.prototype);
/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,7 +51,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


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

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 +73,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 +134,9 @@
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!
43 changes: 35 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
/* 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 - When a function containing "this" is invoked globally, "this" will refer to the global "window" object or will be undefined in "use strict" mode.
* 2. Implicit Binding - When a function is a method of some object, "this" will refer to that object when the function is invoked as a method.
* 3. NEW Object Binding - When a constructor function is created and "this" is used in the body of the constructor function, "this" will refer to any object that is newly created from the constructor class.
* 4. Explicit Binding - When an object is bound to a function using exampleFunction.bind(Object) and that function is called e.g. boundFunction(), "this" will refer to the specified "Object" passed to the bind method.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function defaultBindingFunction() {
console.log(`Default Binding: \"this\" refers to => \"${this.__proto__.constructor.name}\" Object`);
}

// Principle 2
defaultBindingFunction();

// Principle 2
// code example for Implicit Binding
function implicitBindingFunction() {
console.log(`Implicit Binding: \"this\" refers to => \"${this.name}\" Object`);
}

// Principle 3
const implicitObject = {
"name": "implicitObject",
"implicit_object_method": implicitBindingFunction
}

implicitObject.implicit_object_method();

// Principle 3
// code example for New Binding
function NewBindingConstructor(name) {
this.name = name;
console.log(`NEW Binding: \"this\" refers to => \"${this.name}\" Object`);
}

const imANewObject = new NewBindingConstructor("imANewObject");

// Principle 4
// code example for Explicit Binding
function explicitBindingFunction() {
console.log(`Explicit Binding: \"this\" refers to => \"${this.name}\" Object`);
}

const specifiedObject = {
"name": "specifiedObject"
}

// code example for Explicit Binding
const boundFunction = explicitBindingFunction.bind(specifiedObject); // Can use call or apply also
boundFunction();