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
170 changes: 135 additions & 35 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,34 @@
* 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 () {
return `${this.name} took damage.`;
};

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,7 +53,20 @@
* 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.
Expand All @@ -41,70 +75,136 @@

// Test you work by un-commenting these 3 objects and the list of console logs below:

/*
const mage = new Humanoid({
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
'Staff of Shamalama',
],
language: 'Common Tongue',
});
});

const swordsman = new Humanoid({
const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});
});

const archer = new Humanoid({
const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
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(swordsman.destroy()); // Sir Mustachio was removed from the game.
*/
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(mage.name); // Bruce
console.log(swordsman.team); // The Round Table
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(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);
}

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

Hero.prototype.attack = function (opponent, damage) {
opponent.healthPoints -= damage;
if (opponent.healthPoints <= 0) {
opponent.destroy();
return `${this.name} attacked ${opponent.name} with a ${this.weapons[0]} for ${damage} health points and defeated them.`;
} else {
return `${this.name} attacked ${opponent.name} with a ${this.weapons[0]} for ${damage} health points.`;
}
}

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

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

Villain.prototype.attack = function (opponent, damage) {
opponent.healthPoints -= damage;
if (opponent.healthPoints <= 0) {
opponent.destroy();
return `${this.name} slashed ${opponent.name} with a ${this.weapons[0]} for ${damage} health points and defeated them.`;
} else {
return `${this.name} slashed ${opponent.name} with a ${this.weapons[0]} for ${damage} health points.`;
}
}

const hero = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Mermaid Man',
team: 'Mermaid Man and Barnacle Boy',
weapons: [
"Orb of Confusion"
],
language: 'English',
});

const villain = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 1,
height: 5,
},
healthPoints: 10,
name: 'Barnacle Man',
team: 'Every Villain Is Lemons',
weapons: [
"Tickle Belt"
],
language: 'English',
});

console.log(hero.attack(villain, 5));
console.log(villain.attack(hero, 8));
console.log(hero.attack(villain, 5));
26 changes: 21 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global object binding: All Javascript programs take place within the context of a global object. In browsers, this global object is called window, but in node, it is called global. In lieu of any other overriding context, this refers to the global object.
* 2. Implicit Binding: When a function is called using the dot operator on an object, this refers to the object in front of the dot.
* 3. New Binding: When using a constructor function with the new keyword, this refers to the object to be returned by the constructor function.
* 4. Explicit binding: When using the call or apply functions, one must pass the object to which this refers, thus allowing one to use functions belonging to one object on another.
*
* 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
const person = {
"name": "Christian",
"sayHi": function () {
console.log(`Hi, my name is ${this.name}.`);
}
}

person.sayHi();

// Principle 3

// code example for New Binding
function Person(attributes) {
this.name = attributes.name;
}

const anotherPerson = new Person({ "name": "Matthew" });
console.log(anotherPerson.name);

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
person.sayHi.call(anotherPerson);