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
120 changes: 117 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,53 @@
* 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.` };

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

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

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

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

function Hero (Attributes){
this.damage = Attributes.damage;
this.race = Attributes.race;
this.experience = Attributes.experience;
this.gold = Attributes.gold;
Humanoid.call(this, Attributes);
}

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

Hero.prototype.attacked = function(obj){
console.log(`${obj.name} attacks ${this.name}!`);
this.healthPoints = this.healthPoints - obj.damage;
}

function Villain (Attributes){
Hero.call(this,Attributes);
}

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

/*
=== CharacterStats ===
* healthPoints
Expand All @@ -41,7 +88,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,6 +139,48 @@
language: 'Elvish',
});

const hero = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 25,
name: 'Hero',
team: 'Self',
weapons: [
'Sword',
'Axe',
],
language: 'Common',
damage: 5,
race: 'Human',
experience: 0,
gold: 0,
});

const villain = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 5,
height: 4,
},
healthPoints: 15,
name: 'Troll',
team: 'Bridge 22',
weapons: [
'Club',
'Giant Fish',
],
language: 'Troll',
damage: 2,
race: 'Troll',
experience: 25,
gold: 50,
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
Expand All @@ -100,9 +189,34 @@
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(mage.takeDamage(villain)); // Bruce took 2 damage.
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
*/


while (true){
//Troll gets first strike!
hero.attacked(villain); //Troll attacks Hero!
console.log(hero.takeDamage(villain)); //Hero takes 2 damage!
if (hero.healthPoints <= 0){
//Hero has died!
console.log(`${hero.name} has suffered lethal damage!`);
console.log(hero.destroy());
break;
}
//Hero counters!
villain.attacked(hero); //Hero attacks Troll!
console.log(villain.takeDamage(hero)); //Villain takes 5 damage!
if (villain.healthPoints <= 0){
//Troll has died! Yay!
console.log(`${villain.name} has suffered lethal damage!`);
console.log(villain.destroy());
hero.experience += villain.experience;
hero.gold += villain.gold;
console.log(`Thou hast done well in slaying the ${villain.name}! Of experience thou hast gained ${villain.experience}. Of gold thou hast gained ${villain.gold}.`);
break;
}
}


// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
65 changes: 60 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 - "this" is bound to the global/window scope.
* 2. Implicit Binding - "this" is bound to the object preceeding the ".".
* 3. New Binding - "this" is bound to the object created by the "new" keyword.
* 4. Explicit Binding - "this" is bound to the object using .call and .apply.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,69 @@

// code example for Window Binding

function example1(){
document.writeln(this);
return 0;
}example1(); //[object Window]

// Principle 2

// code example for Implicit Binding

const example2 = {
phrase: "I'm example2!",
hello(){
document.writeln(this.phrase)
},
child: {
phrase: "I'm example2's child!",
hello(){
document.writeln(this.phrase)
}
}
}

example2.hello(); //prints "I'm example2!"
example2.child.hello(); //prints "I'm example2's child!"

// Principle 3

// code example for New Binding

function Factory (name, age, favoriteFood){

function describeMe(){
this.name = name;
this.age = age;
this.favoriteFood = favoriteFood;
document.writeln(`My name is ${this.name}, I'm ${this.age} years old and I love ${this.favoriteFood}!`);
}describeMe();
}

const person1 = new Factory("Mike", 32, "Reuben Sandwiches");

// Principle 4

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

function sayHello(){
if (this === rocky){
document.writeln(`Ey yo! I'm ${this.name}! They call me ${this.title}!`);
}
if (this === plissken){
document.writeln(`The name's ${this.name}. Call me ${this.title}.`);
}
}

const rocky = {
name: "Rocky Balboa",
title: "The Italian Stallion"
}

const plissken = {
name: "Plissken",
title: "Snake"
}

sayHello.call(rocky); //Gives Rocky's info to writeln
sayHello.call(plissken); //Gives Snake's info to writeln