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
275 changes: 203 additions & 72 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@

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

/*
=== GameObject ===
* createdAt
* dimensions
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/
function GameObject(optionsObj) {
this.createdAt = optionsObj.createdAt;
this.dimensions = optionsObj.dimensions;
}
GameObject.prototype.destroy = function () {
return `${this.name} was removed from the game.`;
}


/*
=== CharacterStats ===
Expand All @@ -23,6 +31,18 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats(optionsObj) {
GameObject.call(this, optionsObj);
this.hp = optionsObj.hp;
this.name = optionsObj.name;
}

CharacterStats.prototype = Object.create(GameObject.prototype);
CharacterStats.prototype.constructor = CharacterStats;

CharacterStats.prototype.takeDamage = function () {
return `${this.name} took damage`;
}
/*
=== Humanoid ===
* faction
Expand All @@ -32,79 +52,190 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


function Humanoid(optionsObj) {
CharacterStats.call(this, optionsObj);
this.faction = optionsObj.faction;
this.weapons = optionsObj.weapons;
this.language = optionsObj.language;
}

Humanoid.prototype = Object.create(CharacterStats.prototype);
Humanoid.prototype.constructor = Humanoid;

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.
*/
* 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 uncommenting these 3 objects and the list of console logs below:

/*
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
hp: 5,
name: 'Bruce',
faction: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Toungue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
hp: 15,
name: 'Sir Mustachio',
faction: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Toungue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
hp: 10,
name: 'Lilith',
faction: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.hp); // 15
console.log(mage.name); // Bruce
console.log(swordsman.faction); // 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 Villian and Hero constructor functions that inherit from the Humanoid constructor function.
// * Give the Hero and Villians 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 villian and one a hero and fight it out with methods!
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
hp: 5,
name: 'Bruce',
faction: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Toungue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
hp: 15,
name: 'Sir Mustachio',
faction: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Toungue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
hp: 10,
name: 'Lilith',
faction: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.hp); // 15
console.log(mage.name); // Bruce
console.log(swordsman.faction); // 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 Villian and Hero constructor functions that inherit from the Humanoid constructor function.
// * Give the Hero and Villians 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 villian and one a hero and fight it out with methods!

function Hero(optionsObj) {
Humanoid.call(this, optionsObj);
}
Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.constructor = Hero;

Hero.prototype.magicMissile = function (villain) {
villain.hp = villain.hp - (Math.floor(Math.random() * 1) + 1)
console.log(`${this.name} fires a Magic Missile at ${villain.name}`)
}

Hero.prototype.checkHP = function () {
if (this.hp <= 0) {
console.log(`${this.destroy()}`)
} else {
console.log(`${this.name} has ${this.hp} remaining`);
}
}

function Villain(optionsObj) {
Humanoid.call(this, optionsObj)
}
Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.constructor = Villain;

Villain.prototype.acidArrow = function (hero) {
hero.hp = hero.hp - (Math.floor(Math.random() * 2) + 1)
console.log(`${this.name} fires an Acid Arrow at ${hero.name}`);
}

Villain.prototype.checkHP = function () {
if (this.hp <= 0) {
console.log(`${this.destroy()}`);
} else {
console.log(`${this.name} has ${this.hp} remaining`);
}
}

const jace = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 3,
},
hp: 75,
name: 'Jace Beleren',
faction: 'Vryn',
weapons: [
'Tome of Vryn'
],
language: 'Words of the Planeswalkers',
});

const tezzeret = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 5,
},
hp: 115,
name: 'Tezzeret',
faction: 'Alara',
weapons: [
'The Artificer'
],
language: 'Words of the Planeswalkers',
});

function turnSimulator(hero, villain) {
console.log(`Game Start.`)
let winner = null;
for (let i = 99; i > 0; i--) {
if (hero.hp <= 0 || villain.hp <= 0) {
break;
} else {
if ((Math.floor(Math.random() * 100) + 1) % 2 === 0) {
console.log(`It's ${hero.name}'s turn.`)
hero.checkHP();
hero.magicMissile(villain);
villain.checkHP();
console.log(`----end of turn----`)

} else {
console.log(`It's ${villain.name}'s turn.`)
villain.checkHP();
villain.acidArrow(hero);
hero.checkHP();
console.log(`----end of turn----`)
}
}
}
return `Game Over`
}

console.log(turnSimulator(jace, tezzeret));
52 changes: 47 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,68 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. `this` is bound by default to the window object in the global scope. This is window binding. Also referred to as global binding.
* 2. Implicit binding is a default behavior that applies to the block scope use of `this` when dot notation is used to call a method on an object, and refers to the value to the left of the dot.
* 3. Explicit binding is when you verbosely declare via call apply or bind what `this` is.
* 4. A New binding occurs when a constructor is used to create an object, in which case, `this` refers to the new object created when invoking a class constructor function to create a new object from the constructor.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
name = 'global/window'
globalVar = {
'name': 'Some Object'
};
function windowScope() {
console.log(`Hello, I am ${this.globalVar.name}, these are my contents ${this.globalVar}, and I belong to the ${this.name}! I am also accessible through global.globalVar in node, or window.globalVar in your browser. 'this' in the context used here does not refer to the function, but to the function parent, because 'this' is used in the lexical context of window/global, since the function that called this information is in the global/window environment`)
};
windowScope();

// Principle 2

// code example for Implicit Binding

const lexicalContext = {
'name': 'Another Object',
'stats': function() {
console.log(`Hello, I am ${this.name}, and these are my contents ${this}, I may seem like cousin ${globalVar}, but my use of 'this' refers to myself because of being contained within the confines of a lexical context within an object, in short 'this' references the parent of the lexical environment from which 'this' is within`);
let that = this
return function() {
console.log(`${this} is not the parent object anymore. Here 'this' is rather global/window. This behavior is due to being called without context in regards to the parent object. The context is no longer within the object, but within the function that called this function. Due to this, the implicit context no longer applies, and we revert back to the original behavior of 'this'. One way to alleviate 'this' misbehavior is using a closure such as ${that}, where 'that' is a pointer to 'this' as defined in the outer scope`)
};
}
}
lexicalContext.stats()();

// Principle 3

// code example for New Binding
const Constructor = function(name, favoriteSoup) {
this.name = name;
this.favoriteSoup = favoriteSoup;
this.methodSpeak = function() {
if (this.name === "Jason") {
console.log(`Here I am, my name is ${this.name}, and 'this' applies to my specific instance as it is bound when instantiated with the 'new' keyword. I am ${this} not: \n ${this.constructor.name}. \n My favorite soup is ${this.favoriteSoup}`);
} else {
console.log(`My name is ${this.name}, and I am also under the rules of new 'this' binding. I also am not: \n ${this.constructor.name}, but rather my own self ${this}. \n My favorite soup is ${this.favoriteSoup}`);
}
};
}
const constructedJason = new Constructor("Jason", "Broccoli Cheddar");
const constructedPerson = new Constructor("Random", "Gumbo");
constructedJason.methodSpeak();
constructedPerson.methodSpeak();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

const functionSpeak = function(differingParams) {
console.log(`${differingParams} ${this.name}`)
}

functionSpeak("'this' is");
functionSpeak.call(constructedJason, "However, 'this' is")