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
62 changes: 49 additions & 13 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
/*
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.
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.
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:
*/

/*
* 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.
*/

/*
=== 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.`
*/

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,16 +38,46 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

/*
* 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:

/*
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.`
}

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

CharacterStats.prototype = Object.create(GameObject.prototype)
CharacterStats.prototype.takeDamage = function() {
return `${this.name} took damage.`
};

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

Humanoid.prototype = Object.create(CharacterStats.prototype)
Humanoid.prototype.greet = function() {
return `${this.name} offers a greeting in ${this.language}`
}

// new Humanoid({
// name: 'Anthony'
// })

// console.log(Humanoid)

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,7 +138,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
44 changes: 39 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* 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 = targets the window object which is global scope for all of javascript.
* 2. Implicit Binding = when the function is invoked, look to the left of the "."
* 3. Explicit Binding = explicitly saying what "this" is by .call/.apply outside the scope of the function
* 4. New Binding = constructor functions are capitalized and invoked with "new" keyword, its bound to the "this" of "new".
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,48 @@

// code example for Window Binding

var age = function() {
console.log(this.age); //targets the window
}

// Principle 2

// code example for Implicit Binding
var myName = {
name: 'Anthony',
age: 29,
sayName: function() {
return this.name; //left of "." is "this" = myName
}
};

console.log(myName.sayName()); //left of "." is ""

// Principle 3

// code example for New Binding

var Animal = function(color, type, name){
this.color = color;
this.type = type;
this.name = name;
console.log(`This animal is a ${this.name}, a type of ${this.type}, colored ${this.color}.`)
}

var dove = new Animal('white', 'bird', 'dove');


// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
var sayName = function() {
console.log(`My name is ${this.name}.`);
};

var me = {
name: 'Anthony',
age: 29,
};


console.log(sayName.call(me));