Skip to content
Merged
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
194 changes: 122 additions & 72 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 @@ -32,79 +32,129 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

/*
* 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.
*/
* 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: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});

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

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: '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.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.
*/
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.`;
}

function CharacterStats(attr) {
this.healthPoints = attr.healthPoints;
}
CharacterStats.prototype = Object.create(GameObject.prototype);
CharacterStats.prototype.takeDamage = function(){
return `${this.name} took damage.`;
}

function Humanoid(attr){
this.team = attr.team;
this.weapons = attr.weapons;
this.language = attr.language;
GameObject.call(this, attr);
CharacterStats.call(this, attr);
}
Humanoid.prototype = Object.create(CharacterStats.prototype)

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


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

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

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4
},
healthPoints: 10,
name: "Lilith",
team: "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.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.

function Villain(attr){
this.lair = attr.lair;
this.victimCount = attr.victimCount;
this.adjective = attr.adjective;
Humanoid.call(this, attr);
}

Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.evile = function() {
return `${this.name} the ${this.adjective} murders ${this.victimCount} babies in ${this.lair}.`
}

const baddie = new Villain ({
createdAt: new Date(),
healthPoints: 10,
name: "Milton",
team: "Der",
weapons: ["Axe of Hell"],
language: "Stthmgol",

adjective: "Mad",
victimCount: 1741,
lair: "The Village of the Damned"
})

console.log(baddie.evile()); // Bruce took damage.


// 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!
// * 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!
53 changes: 46 additions & 7 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Global - without context, `this` is in the context of either the Global or Window object
* 2. Implicit - when using an object method, `this` context is implied to be the object left of the dot
* 3. New - when using a constructor, `this` context is of the object built by the constructor
* 4. Explicit - when using bind/apply/call, `this` context explicitly defined.
"half stretch" answer to all 4: `this` generally refers to the parent context of where it's used.
In the case of Explicit, the value of `this` is overridden to be what you give it.
(give example of something about Array())
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(this.Array.prototype) // refers to array on the Window (in this case) object.

// Principle 2

// code example for Implicit Binding
let kids = {
first: "Schuyler",
second: "Bennett",
third: "Valkyrie",
fourth: "Maxx",
fifth: "DeLenn",
middle: function(){
console.log(`middle kid is ${this.third}`)
}
}
kids.middle()

// Principle 3

// code example for New Binding
function Kids (incoming) {
this.incoming = incoming, // this `this` threw me for a minute
this.first = "Schuyler",
this.second = "Bennett",
this.third = "Valkyrie",
this.fourth = "Maxx",
this.fifth = "DeLenn",
this.favorite = function(){
console.log(`My favorite kid is ${this.incoming}`);
};
}

const best = new Kids('all of em.');
best.favorite();


// Principle 4
// code example for Explicit Binding

function car(){
console.log(`${this.model}`);
}

const carData = {
make: "Honda",
model: "Civic",
year: "2005",
}

// code example for Explicit Binding
car.call(carData);