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
103 changes: 87 additions & 16 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Each constructor function has unique properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +16,33 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(character) {
this.createdAt = character.createdAt;
this.name = character.name;
this.dimensions = character.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(character) {
GameObject.call(this, character);
this.healthPoints = character.healthPoints;
}

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 +52,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


function Humanoid(character) {
CharacterStats.call(this, character);
this.team = character.team;
this.weapons = character.weapons;
this.language = character.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 +74,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 @@ -91,20 +124,58 @@
],
language: 'Elvish',
});
console.log('Hello');
console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
console.log(mage.weapons); // Staff of Shamalama
console.log(archer.language); // Elvish
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.

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
console.log(mage.weapons); // Staff of Shamalama
console.log(archer.language); // Elvish
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.
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
function Villain(character) {
Humanoid.call(this, character);
this.attack = function() {
let damage = Math.floor(Math.random() * 11) // random number between 0 and 10
}
this.checkDeath = function() {
if (this.healthPoints <= 0) {
this.destroy();
} else {
this.attack();
}
}
}

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

function Hero(character) {
Humanoid.call(this, character);
}

Hero.prototype = Object.create(Villain.prototype);
// * 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!

const mainVillain = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});
37 changes: 32 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* 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 - if no rules apply, the this keyword defaults to the Window object,
* unless we specify strict mode, then it returns undefined
* 2. Impilicit Binding - automatic binding to whatever is on the left will be refered to as this
* 3. Explicit Binding - explicitly telling the JavaScript engine to point to a specific value using
* call, apply, or bind
* 4. New Binding - use the new keyword and it will refer to that instance of the object
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +15,39 @@

// code example for Window Binding

console.log(this) // Will log the entirety of the window object

// Principle 2

// code example for Implicit Binding

let Travis = {
name: 'Travis',
last: 'Laudahl',
birthYear: 1995,
age: function() {
return `${this.name} is ${2019-this.birthYear} years old`; // Implicit - this. refers to the Travis object
}
};

// Principle 3

// code example for New Binding

function FullName(name) {
this.name = name;
this.sport = 'fishing';
};

let newTravis = new FullName('Travis Laudahl'); // Creates a newTravis object that will reference the FullName object

console.log(newTravis.name);

// Principle 4
// code example for Explicit Binding

function logSport() {
console.log(this.sport);
}

// code example for Explicit Binding
logSport.call(newTravis); // 'this' will refer to the FullName Object