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
116 changes: 112 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,35 @@
* 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.`);
}

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats (characterAttributes) {
GameObject.call(this, characterAttributes);
this.healthPoints = characterAttributes.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,6 +54,21 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(humanoidAttributes) {

CharacterStats.call(this, humanoidAttributes);
this.team = humanoidAttributes.team;
this.weapons = humanoidAttributes.weapons;
this.language = humanoidAttributes.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 +78,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 All @@ -58,7 +95,7 @@
language: 'Common Tongue',
});

const swordsman = new Humanoid({
const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
Expand Down Expand Up @@ -102,9 +139,80 @@
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 Villain(villainAttributes){

Humanoid.call(this, villainAttributes);
this.laugh = villainAttributes.laugh;
this.stature = villainAttributes.stature;

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

Villain.prototype.decrementVillianPoints = function() {
this.healthPoints = this.healthPoints - 1;
console.log(`The villain has taken 1 dmg! This is his current HP ${this.healthPoints}`);
}

function Hero(heroAttributes) {
Humanoid.call(this, heroAttributes);
this.nobility = heroAttributes.nobility;
this.aura = heroAttributes.aura;
}

Hero.prototype.decrementHeroPoints = function() {
this.healthPoints = this.healthPoints - 5;
console.log(`The villain has taken 1 dmg! This is his current HP ${this.healthPoints}`);
}

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

const Guts = new Hero({

createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Black Swordsman',
team: 'Forest Kingdom',
weapons: [
'Sword',
'Cannon',
],
language: 'Elvish',

});

const Griffith = new Villain({

createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10000,
name: 'Femto',
team: 'Demon Army',
weapons: [
'Bow',
'Dagger',
],
language: 'All',

});

Guts.decrementHeroPoints();

for(let i = 0; i < 10; i++)
Griffith.decrementVillianPoints()

Guts.decrementHeroPoints();
27 changes: 22 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 binding done to connect to the main window of the javascript language. You will get all of the default variables and functions found with the language by tieing this to the console value.
* 2. Implicit Binding - Is binding where the this keyword ties to the object before the dot.
* 3. New Binding - this a type of binding, which is the creation of a new object which has the properties of the object that is to the left hand side of the equal sign
* 4. Explicit Binding - this is when you have a function call a this. You are invoking this through a function call
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,31 @@

// code example for Window Binding

console.log(this);

// Principle 2

// code example for Implicit Binding

function conceptObject(conceptAttribute){

this.value = conceptAttribute.value;
}

conceptObject.prototype.speak = function() {

return(this.value);
}

// Principle 3

// code example for New Binding

const testNew = new conceptObject({
value: 15
});

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
console.log(testNew.speak());