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
113 changes: 110 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,35 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(attr) {
this.createdAt = attr.createdAt;
this.name = attr.name;
this.dimensions = attr.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(attr) {
GameObject.call(this, attr);
this.healthPoints = attr.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.) ===
Expand All @@ -32,6 +55,21 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attr) {
CharacterStats.call(this, attr);
this.team = attr.team;
this.weapons = attr.weapons;
this.language = attr.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 +79,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 @@ -102,9 +140,78 @@
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(attr) {
Humanoid.call(this, attr);
this.state = attr.state;
this.weaponDamage = attr.weaponDamage;
this.role = attr.role;
}

function Hero(attr) {
Humanoid.call(this, attr);
this.state = attr.state;
this.weaponDamage = attr.weaponDamage;
this.role = attr.role;
}

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



const hero = new Hero({
healthPoints: 100,
dimensions: {
length: 1,
width: 3,
height: 5
},
name: 'Jopay',
role: 'Hero',
weapons: ['Sword', 'Shield'],
weaponDamage: 50,
state: "alive",
language: 'Knightish'
})

const villian = new Hero({
healthPoints: 100,
dimensions: {
length: 1,
width: 3,
height: 5
},
name: 'Manhunt',
role: 'Villian',
weapons: ['Dagger', 'Bow'],
weaponDamage: 100,
state: "alive",
language: 'Lowkey'
})

const hit_character = function(character) {
if (character.state == "alive") {
character.healthPoints = character.healthPoints - character.weaponDamage;
console.log("Hit!", 'Health is: ' + character.healthPoints);

if(character.healthPoints <= 0) {
console.log(`${character.name} our ${character.role} DIED!!!`);
character.state = "dead";
} else {
console.log(`${character.name} our ${character.role} still ${character.state}!!!`);
}
}
else {
console.log('already dead');
}
}


hit_character(hero);
hit_character(villian);
46 changes: 40 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
/* 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 basically referencing to the object that the function is called or executed.
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function client(name) {
console.log(this);
}

client('Dennis');

// Principle 2

// code example for Implicit Binding
const newClient = {
name: 'James',
age: 29,
code: function() {
return `${this.name} age is ${this.age} and he's one of the top CEO's.`;
}
}

console.log(newClient.code());

// Principle 3

// code example for New Binding
function ClientRole(name, jobrole) {
this.name = name;
this.jobrole = jobrole;
this.speak = function() {
console.log(`${this.name} is the ${this.jobrole} of the company`);
}
}

const James = new ClientRole('James', 'CEO');
James.speak();


// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
const clientInfo = {
"name": "james",
"role": "CEO",
"phone": "608-123-4567"
}

const companiesBuilt = ['Tech Industry LLC', 'Testing Water LLC', 'Just a company LLC'];

function speak(company1, company2, company3) {
return `${this.name} is the ${this.role} of the company. He built the following: ${company1}, ${company2}, and ${company3}`;
}

console.log(speak.call(clientInfo, ...companiesBuilt));