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
112 changes: 109 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,51 @@
* 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(gameStuff) {
this.createdAt = gameStuff.createdAt;
this.name = gameStuff.name;
this.dimensions = gameStuff.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(charAttributes) {
GameObject.call(this, charAttributes);
this.healthPoints = charAttributes.healthPoints;
}

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

CharacterStats.prototype.takeDamage = function() {
return this.name + " took damage.";
}

CharacterStats.prototype.attack = function(target) {
this.attackDmg = Math.floor((Math.random() * 50) + 1);
if(this.healthPoints > 0 && target.healthPoints > 0)
console.log(this.name + " attacks " + target.name + " with " + this.weapons + " for " + this.attackDmg + " points of damage.");

if (this.healthPoints <= 0) {
return this.name + " cannot attack, it is dead."
} else if (target.healthPoints <= 0) {
return this.name + " beats up " + target.name + "'s dead body."
} else {
target.healthPoints = target.healthPoints - this.attackDmg;
if (target.healthPoints <= 0 )
return this.name + " has killed " + target.name + "!";

else
return target.name + " is still alive with " + target.healthPoints + " health points left"
}
}

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
Expand All @@ -32,6 +70,18 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(humAttributes) {
CharacterStats.call(this, humAttributes);
this.team = humAttributes.team;
this.weapons = humAttributes.weapons;
this.language = humAttributes.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
Expand All @@ -41,7 +91,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 +142,52 @@
language: 'Elvish',
});


function Villian(villStats) {
CharacterStats.call(this, villStats);
this.name = villStats.name;
this.healthPoints = villStats.healthPoints;
this.weapons = villStats.weapons;
this.language = villStats.language;
this.attackDmg = villStats.attackDmg;
this.remainingHealth = villStats.remainingHealth;
}

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

const javascript = new Villian({
name: 'Javascript',
healthPoints: 100,
weapons: 'Demoralization',
language: 'idfk',
attackDmg: Math.floor((Math.random() * 50) + 1),
remainingHealth: 100
});



function Hero(heroStats) {
CharacterStats.call(this, heroStats);
this.name = heroStats.name;
this.healthPoints = heroStats.healthPoints;
this.weapons = heroStats.weapons;
this.remainingHealth = heroStats.remainingHealth;
this.attackDmg = heroStats.attackDmg;
}

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

const web21 = new Hero({
name: 'Web21',
healthPoints: 100,
weapons: 'Josh Knell',
attackDmg: Math.floor((Math.random() * 50) + 1),
remainingHealth: 100
});




console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
Expand All @@ -102,9 +198,19 @@
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
console.log(web21.attack(javascript));
console.log(javascript.attack(web21));
console.log(web21.attack(javascript));
console.log(javascript.attack(web21));
console.log(web21.attack(javascript));
console.log(javascript.attack(web21));
console.log(web21.attack(javascript));
console.log(javascript.attack(web21));

// 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!

44 changes: 35 additions & 9 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
* in your own words. explain the four principles for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global binding - Binds "this" to the console object.
* 2. Implicit Binding - Binds "this" to the object that is proceeded by a . and a function.
* 3. New binding - Binds object to constructor function using "new"
* 4. Explicit binding - Happens when .call(), .apply(), or .bind() are used on a function. It is explicit because you need to explicitly pass in a "this" context.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding

// Principle 2
someGreeting = function(name) {
return "Window/Global Binding: Hello my name is " + this;
}
console.log(someGreeting("Justin"));

// Principle 2
// code example for Implicit Binding
const myObject = {
name: 'Justin',
sayHello: function() {
console.log("Implicit Binding: Hello my name is " + this.name);
}
}

// Principle 3
myObject.sayHello('Justin');

// Principle 3
// code example for New Binding

function AnotherPerson(person) {
this.sayHello = "Hello ";
this.name = person;
this.greet = function() {
console.log("New Binding: " + this.sayHello + this.name); // 'this' will be binded to whatever we throw in as an argument to AnotherPerson
}
this.explicitGreet = function() {
console.log("Explicit Binding: " + this.sayHello + this.name);
}
}

const justin = new AnotherPerson('Justin');
const xavier = new AnotherPerson('Xavier');
justin.greet();

// Principle 4
// code example for Explicit Binding

// code example for Explicit Binding
justin.explicitGreet.call(xavier);