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
234 changes: 166 additions & 68 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@

Each class has unique properites and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
* dimensions
* destroy() // prototype method -> returns the string 'Object was removed from the game.'
*/
function GameObject(myObj) {
this.createdAt = myObj.createdAt
this.dimensions = myObj.dimensions
}

GameObject.prototype.destroy = function () {
return `${this.name} was removed from the game.`
}
/*
=== CharacterStats ===
* hp
Expand All @@ -23,6 +30,19 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats(myObj) {
GameObject.call(this, myObj)
this.hp = myObj.hp
this.name = myObj.name
}
CharacterStats.prototype = Object.create(GameObject.prototype);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect use of the call and Object create combo. 💯


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



/*
=== Humanoid ===
* faction
Expand All @@ -32,7 +52,23 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


function Humanoid(myObj) {
CharacterStats.call(this, myObj)
this.faction = myObj.faction

@ghost ghost Apr 19, 2018

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this really can getcha, if you need semi-colons feel free to ask and I will lend you some. Worst case you find an error down the road that probably even tells you where an assignment went wrong...

// This is/should be valid javascript
let a, b, c;
a = b = c = 1;
// ===
a = b
c = 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this behavior does right itself when typed in the browser console. I wouldn't rely on it myself.

this.weapons = myObj.weapons
this.language = myObj.language
}

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

Humanoid.prototype.greet = function () {
return `${this.name} offers a greeting in ${this.language}`
}



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

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

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

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

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

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.hp); // 15
console.log(mage.name); // Bruce
console.log(swordsman.faction); // 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 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!
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
hp: 5,
name: 'Bruce',
faction: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Toungue',
});

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

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

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

function Villain(myObj) {
Humanoid.call(this, myObj)
}
Villain.prototype = Object.create(Humanoid.prototype)
Villain.prototype.removeHealth = function () {
if (this.hp < 1) {
return this.destroy();
}

return --this.hp
}

function Hero(myObj) {
Humanoid.call(this, myObj)
}
Hero.prototype = Object.create(Humanoid.prototype)
Hero.prototype.removeHealth = function () {
if (this.hp < 1) {
return this.destroy();
}

return --this.hp
}

const hades = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
hp: 1,
name: 'Hades',
faction: 'Underworld',
weapons: [
'Magic',
],
language: 'Greek',
})

const hercules = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
hp: 1,
name: 'Hercules',
faction: 'Olympus',
weapons: [
'Bow',
'Sword',
],
language: 'Greek',
})

console.log("********Strech Test********");
console.log(hades.removeHealth());
console.log(hades.removeHealth());
console.log(hercules.removeHealth());
console.log(hercules.removeHealth());
46 changes: 41 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
/* 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
* 2. Implicit
* 3. Explicit
* 4. New Binding
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
// window.name = 'Brandon';
// function wind(name) {
// console.log(this);
// return name;
// }

// Principle 2

// code example for Implicit Binding
const newObj = {
prop1: 'prop1',
logProp: function() {
return this.prop1;
}
}
console.log(newObj.logProp());

// Principle 3

// code example for New Binding
// function Person(attr) {
// this.name = attr.name;
// this.age = attr.age;
// this.speak = function() {
// return `My name is ${this.name} and I am ${this.age} years old`;
// };
// }
// const brandon = new Person({
// name: 'brandon',
// age: 28
// });
// console.log(brandon.speak());

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
const brandon = {
name: 'Brandon',
age: 28
};

const greet = function(skill1, skill2, skill3) {
return `Hello my name is ${this.name} and I am ${this.age} and I like to ${skill1}, ${skill2} and ${skill3}`;
};

const skills = ['walk', 'talk', 'chew gum'];

console.log(greet.call(brandon, ...skills));