Skip to content
Closed
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
135 changes: 132 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
* destroy() // prototype method -> returns the string 'Object was removed from the game.'
*/

function GameObject (attributes) {
this.createdAt = new Date(),
this.dimensions = {
length: attributes.dimensions.length,
width: attributes.dimensions.width,
height: attributes.dimensions.height
}
}

GameObject.prototype.destroy = function() {
return "Object was removed from the game.";
};

/*
=== CharacterStats ===
* hp
Expand All @@ -23,6 +36,20 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats (characterAttributes) {
GameObject.call(this, characterAttributes);
this.hp = characterAttributes.hp;
this.name = characterAttributes.name
}

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


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

CharacterStats.prototype.destroy = GameObject.prototype.destroy;
/*
=== Humanoid ===
* faction
Expand All @@ -33,6 +60,24 @@
* should inherit takeDamage() from CharacterStats
*/

function Humanoid (humanoidAttributes) {
CharacterStats.call(this, humanoidAttributes);
this.faction = humanoidAttributes.faction,
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}.`;
}

Humanoid.prototype.destroy = CharacterStats.prototype.destroy;

Humanoid.prototype.takeDamage = CharacterStats.prototype.takeDamage;

/*
* Inheritance chain: Humanoid -> CharacterStats -> GameObject
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +86,7 @@

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

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,9 +147,93 @@
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 Villian and Hero classes that inherit from the Humanoid class.
// * Give the Hero and Villians 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 villian and one a hero and fight it out with methods!
// * Create two new objects, one a villian and one a hero and fight it out with methods!


// Hero
function Hero (heroAttributes) {
Humanoid.call(this, heroAttributes);

}

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

Hero.prototype.attack = function() {
return `${this.name} deals 2 points of damage with ${this.weapons}!`
}

// Villian
function Villian (villanAttributes) {
Humanoid.call(this, villanAttributes);
}

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

Villian.prototype.attack = function() {
return `${this.name} deals 3 points of damage with ${this.weapons}!`
}

Villian.prototype.dies = function() {
return this.name + ' has died!'
}

// Hero and Villian Objects

const King = new Hero({
createdAt: new Date(),
dimensions: {
length: 3,
width: 3,
height: 4,
},
hp: 20,
name: 'Arthur',
faction: 'Kights of the Round Table',
weapons: [
'Excaliber',
],
language: 'Old English',
});


const Dragon = new Villian({
createdAt: new Date(),
dimensions: {
length: 30,
width: 30,
height: 40
},
hp: 40,
name: 'Leviathan',
faction: 'Dragons have no factions',
weapons: [
'Fire Breath',
],
language: 'Dragon Language',
});


console.log(King.attack());
console.log(Dragon.takeDamage());
console.log(King.attack());
console.log(Dragon.takeDamage());
console.log(King.attack());
console.log(Dragon.takeDamage());
console.log(King.attack());
console.log(Dragon.takeDamage());
console.log(King.attack());
console.log(Dragon.takeDamage());
console.log(Dragon.attack());
console.log(King.takeDamage());
console.log(Dragon.attack());
console.log(King.takeDamage());
console.log(Dragon.attack());
console.log(King.takeDamage());
console.log(King.attack());
console.log(Dragon.dies());
console.log(Dragon.destroy());
45 changes: 40 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
/* 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.
* 2. Implicit Binding.
* 3. New Binding.
* 4. Explicit Binding.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
let saySomething = function() {
console.log(this.comment);
}

let me = {
comment: "I am learning"
}

saySomething.call(me);
// Principle 2

// code example for Implicit Binding
let school = {
name: 'Lambda',
students: 4000,
age: 5,
schoolName: function () {
console.log(this.name);
}
}

school.schoolName();

// Principle 3

// code example for New Binding
let Truck = function(color, name, type) {
this.color = color;
this.name = name;
this.type = type;
}

let RAM = new Truck('red', 'RAM 2500', 'work truck');

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
let greeting = function () {
console.log("Hello there, my name is " + this.name);
}

let Jose = {
name: 'Jose',
age: 35
};

greeting.call(Jose);