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
40 changes: 28 additions & 12 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/*
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:
Expand All @@ -15,14 +13,28 @@
* 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(attributes) {
this.createdAt = attributes.createdAt;
this.name = attributes.name;
this.dimensions = attributes.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 (charAttributes) {
this.healthPoints = charAttributes.healthPoints;
GameObject.call(this, charAttributes);
}
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,16 +44,22 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid (humAttributes) {
this.team = humAttributes.team;
this.weapons = humAttributes.weapons;
this.language = humAttributes.language;
CharacterStats.call(this, humAttributes);
}
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.
* 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: {
Expand All @@ -57,7 +75,6 @@
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand All @@ -74,7 +91,6 @@
],
language: 'Common Tongue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,8 +118,8 @@
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;
Expand Down
65 changes: 58 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,77 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. When in the global scope, the value of "this" will be the window/console Object.
* 2. "This" refers whenever a function is called by a preceding dot notation, the object left of the dot notation gets "this".
* 3. Whenever a constrctor function is used, "this" refers to the specific instance of the object that is created and returned by the constructor function.
const Kenneth = new Greet('Marie'); ***this refers to "new"***
* 4. Whenever JavaScript's "call" or "apply" method is used, "this" is explicitly defined.
Kenneth.speak.call(Marie); ***this refers method to .call***
Marie.speak.apply(Kenneth); ***this refers method to .apply***
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
// Window Binding - Global Scope Binding

// if "use strict" is active it prevents us from accidentally binding to the window
// return `Hello ${this.name}`; output will be a window/global binding - Do not do!!

function sayFirstName(name) {
console.log(this); // this console.log window
return name;
}
console.log(sayFirstName("Kenneth"))

// Principle 2

// code example for Implicit Binding
// Implicit Binding (Automatic) with objects and methods
const hobbit = {
name: "Gollum",
age: 589,
food: "raw meat",
eat: function() {
return `${this.name} loves to eat ${this.food}`;
}
};

const newHobbit = {
name: "Samwise",
age: 102,
food: "potatoes",
eat: function() {
return `${this.name} loves to eat ${this.food}`;
}
};

console.log(hobbit.eat());
console.log(newHobbit.eat());

// Principle 3

// code example for New Binding

function Greet(name) {
this.greeting = 'Hello ';
this.name = name;
this.speak = function() {
console.log(this.greeting + this.name);
console.log(this);
};
}

const Kenneth = new Greet('Marie');
const Marie = new Greet('Kenneth');

Kenneth.speak();
Marie.speak();

// Principle 4

// code example for Explicit Binding
// Explicit Binding is for functions only // used new Bindings example function
// .apply .call

Kenneth.speak.call(Marie);
Marie.speak.apply(Kenneth);