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
146 changes: 141 additions & 5 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

/*
=== GameObject ===
* createdAt
Expand All @@ -16,13 +16,36 @@
* 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(attributes){
GameObject.call(this,attributes);
this.healthPoints = attributes.healthPoints;

}

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

CharacterStats.prototype.takeDamage = function (num){
return `${this.name} took ${num} damage points.`
};


/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,16 +55,32 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


function Humanoid(attributes) {
CharacterStats.call(this,attributes);
this.team = attributes.team;
this.weapons = attributes.weapons;
this.language = attributes.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
* 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 un-commenting these 3 objects and the list of console logs below:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,9 +141,106 @@
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(attributes) {
Humanoid.call(this, attributes);

this.element = attributes.element;
this.glory = attributes.glory;
}

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

Hero.prototype.lightBlast = function(opponent){
let lightEnvelope = opponent.healthPoints - 5
if( opponent.healthPoints <= 5){
return opponent.destroy()
}else{
opponent.healthPoints = lightEnvelope
return `${opponent.name}'s health decreased to ${lightEnvelope}. ${opponent.name} is trapped in an envelope of light.`
}

}


function Villain(attributes){
Humanoid.call(this, attributes);
this.scorn = attributes.scorn;
}

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

Villain.prototype.TenebrousManifestation = function(opponent){
let shadowbite = opponent.healthPoints - 2;
if(opponent.healthPoints <= 2){
return opponent.destroy()
}else{
opponent.healthPoints = shadowbite;
return `${opponent.name} was bit by the shadow! ${opponent.name}'s health decreased to ${shadowbite}.`
}
}

Villain.prototype.subterfugalAid = function(opponent){
let deceive = opponent.healthPoints + 3;
opponent.weapons = opponent.weapons.shift()
return `${opponent.name}'s health increased to ${deceive}! ...What?! ${this.name} stole one of your weapons!`

}


const lightningMage = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 4,
height: 8,

},
healthPoints: 18,
name: 'Raiden',
team: 'Temple of Asgard',
weapons: [
'Superconductive Staff',
'Lightning',
],
language: 'Ancient in origin',
element: 'lightning',
glory: 5
// the higher the glory the more altruism you spread to others allowing them to share healthpoints and weapons
});

const shadowBinder = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 3,
height: 6,

},
healthPoints: 15,
name: 'Vakoma',
team: 'House of the Undying',
weapons: [
'Sleeve Conceiler'
],
language: 'Worm Tongue',
element: 'Shadow',
scorn: 7
//the higher the scorn the more strife you sew, further enabling deception
});

//Raiden the lightning-mage and Lilith the archer VS Vakoma the shadow-binder

console.log(lightningMage.lightBlast(shadowBinder));
console.log(shadowBinder.subterfugalAid(archer));
console.log(shadowBinder.TenebrousManifestation(lightningMage));
console.log(shadowBinder.subterfugalAid(lightningMage));
console.log(lightningMage.lightBlast(shadowBinder));

//To Be Continued...
42 changes: 34 additions & 8 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.
*
* 1.
* 2.
* 3.
* 4.


* 1. the default object that this refers to, the entire browser window

* 2. the use of dot notation employs this. as a reference to the object that encloses your current scope

* 3. after this. has been implicitly bound within the context of a class constructor object, the keyword 'new' subsequently binds this. to any newly declared objects derived from the class constructor

* 4. assigning this. to an object of your choosing
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(this)

// Principle 2

// code example for Implicit Binding
kennan.speak = function(){
return `Hello, my name is ${this.name}!`
}

// Principle 3

// code example for New Binding
function Coffee(region, roast, aromatics) {
this.region = region,
this.roast = roast,
this.aromatics = aromatics
}

let favoriteCoffee = new Coffee('Ethiopian', 'light roast', 'floral');

console.log(favoriteCoffee)

// Principle 4

// code example for Explicit Binding
let myAnimal ={
name: 'Orca',
age: 33,
sound: 'mooo'
}

function orcaSound(){
console.log(this.sound)
}

let newOrcaSound = orcaSound.bind(myAnimal);
newOrcaSound();