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
107 changes: 93 additions & 14 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,30 @@
* 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(attribute) {
this.createdAt = attribute.createdAt;
this.name = attribute.name;
this.dimensions = attribute.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(characterAttribute) {
GameObject.call(this, characterAttribute);
this.healthPoints = characterAttribute.healthPoints;
CharacterStats.prototype.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 +48,18 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(humanoidAttribute) {
CharacterStats.call(this, humanoidAttribute);
GameObject.call(this,humanoidAttribute);
this.team = humanoidAttribute.team;
this.weapons = humanoidAttribute.weapons;
this.language = humanoidAttribute.language;
Humanoid.prototype.greet = function () {
return `${this.name} offers a greeting in ${this.language}.`;
}
}

Humanoid.prototype = Object.create(CharacterStats.prototype);
/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +68,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 @@ -92,19 +119,71 @@
language: 'Elvish',
});

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.
// * 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!
function Hero(heroAttr) {
Humanoid.call(this, heroAttr);
Hero.prototype.attack = function (enemyName) {
enemyName.healthPoints = enemyName.healthPoints - 3;
if (enemyName.healthPoints > 0) {
return `${enemyName.takeDamage()} from your ${this.weapons}! They have ${enemyName.healthPoints} health remaining.`
}
else {
return `${enemyName.destroy()} You won the battle!`
}
};
}
Hero.prototype = Object.create(Humanoid.prototype);

function Villain(villainAttr) {
Humanoid.call(this, villainAttr);
Villain.prototype.magic = function (enemyName) {
enemyName.healthPoints = enemyName.healthPoints - 3;
if (enemyName.healthPoints > 0) {
return `${enemyName.takeDamage()} from your ${this.weapons}! They have ${enemyName.healthPoints} health remaining.`
}
else {
return `${enemyName.destroy()} You won the battle!`
}
}
}
Villain.prototype = Object.create(Humanoid.prototype);

const theFonz = new Hero({
healthPoints: 10,
name: 'Arthur Herbert Fonzarelli',
team: 'Fonzi',
weapons: [
'Finger Guns'
],
language: 'Aaaaayyyy',
});

const badSanta = new Villain({
healthPoints: 4,
name: 'Chris Kringle',
team: 'Winter is coming!',
weapons: [
'Reindeer Rampage'
],
language: 'North Polish',
})

console.log(badSanta.magic(theFonz));
console.log(theFonz.attack(badSanta));
console.log(badSanta.magic(theFonz));
console.log(theFonz.attack(badSanta));
35 changes: 27 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
/* 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/ GLOBAL binding: Refers to 'this' as being the global object. If I were to use "this" in the global binding, "this" would refer to the actual JavaScript code/object.
* 2. - Implicit binding: "this" refers to the object a method is being called on... me.toUpperCase() - this would refer to 'me'.
* 3. - New binding: In a new binding 'this' is referenced to the variable the new binding is being assigned to... const me = new Child(), 'this refers to the 'me' variable.
* 4. - Explicit binding: 'this' would refer to the object being bound... me.call(you), 'this' is refering to 'you' in this instance.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function hello (name) {
return name + this;
}

// Principle 2

// code example for Implicit Binding
const we = {
first: 'me',
second: 'us',
third: function youAndI(name) {
return `${this.first} and ${name} makes "We"`;
}
};
we.third('Joe');


// Principle 3

// code example for New Binding
function WhoIsTalking(name) {
this.name = name;
this.mood = function () {
return `${this.name} Hangery!`;
}
}
const tj = new WhoIsTalking('TJ');
const bret = new WhoIsTalking('Bret');
bret.mood();
tj.mood();

// Principle 4

// code example for Explicit Binding
console.log(bret.mood.call(tj))