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
Binary file added .vs/JavaScript-III/v16/.suo
Binary file not shown.
8 changes: 8 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ExpandedNodes": [
"",
"\\assignments"
],
"SelectedNode": "\\assignments\\prototypes.js",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
149 changes: 136 additions & 13 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,35 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(gameAttribs) {
this.createdAt = gameAttribs.createdAt;
this.name = gameAttribs.name;
this.dimensions = gameAttribs.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
*/

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

function CharacterStats(charAttribs) {
this.healthPoints = charAttribs.healthPoints;
GameObject.call(this, charAttribs);
};

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

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,6 +54,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

// Humanoid.prototype.takeDamage = Object.create(CharacterStats.prototype.takeDamage);
Humanoid.prototype = Object.create(CharacterStats.prototype);

function Humanoid(humanAttribs) {
CharacterStats.call(this, humanAttribs);
this.team = humanAttribs.team;
this.weapons = humanAttribs.weapons;
this.language = humanAttribs.language;
};

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

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +77,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 @@ -92,19 +128,106 @@
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!

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

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

Villain.prototype.shoot = function () {
if (Math.random() > 0.5) {
if (chris.healthPoints > 0) {
chris.healthPoints -= 5;
return `${this.name} shoots his ${this.weapons[0]} at ${chris.name} for 5 hit points!`;
} else if (chris.healthPoints <= 0) {
console.log(chris.destroy());
return `${this.name} has slain ${chris.name}`;
}
} else {
return `${this.name} shoots his ${this.weapons[0]} and misses.`;
}
}

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

function Hero(heroAttribs) {
Humanoid.call(this, heroAttribs);
}

Hero.prototype.swing = function () {
if (Math.random() > 0.5) {
if (joe.healthPoints > 0) {
joe.healthPoints -= 5;
return `${this.name} swings his ${this.weapons[0]} at ${joe.name} for 5 hit points!`;
} else if (joe.healthPoints <= 0) {
console.log(joe.destroy());
return `${this.name} has slain ${joe.name}`;
}
} else {
return `${this.name} swings his ${this.weapons[0]} and misses.`;
}
}

const chris = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 20,
name: 'Christopher',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const joe = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 15,
name: 'Joseph',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

function battle() {
if (chris.healthPoints > 0) {
console.log(chris.swing());
console.log(joe.shoot());
} else {
console.log(`${chris.name} is dead and can't do anything.`)
}
}

for (i = 0; i < 10; i++) {
battle();
}
58 changes: 53 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* 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 is when 'this' is called outside of an object or a function it binds to the window.
* 2. Implicit binding means 'this' is whatever function or object above the one it is called within, or is to the 'left'.
* 3. New binding means that whenever you invoke the new function javascript will create a new object and call it 'this'.
* 4. Explicit binding means we are specifing what we want 'this' to be using the functions apply, call, and bind.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,62 @@

// code example for Window Binding

console.log(this);
function speak(words) {
"use strict";
console.log(this);
return `${words}`;
}
console.log(speak('HELLO!'));

// Principle 2

// code example for Implicit Binding

const troll = {
name: 'William',
age: 1000,
food: 'People',
cook: function () {
return `${this.name} likes to cook ${this.food}`;
}
}
console.log(troll.cook());

// Principle 3

// code example for New Binding

const box = new Box('param');
function Box(contents) {
this.contents = contents;
this.openup = function() {
console.log(`This box contains ${this.contents}.`);
}
}
const wivesGift = new Box('a gold ring');
console.log(wivesGift);
wivesGift.openup();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

const person = {
name: 'Christopher'
}
const skills = ["Walk", "Run", "Drive", "Swim"];
const skill = "Fly";
function doStuff(thiss, that, thing, other) {
"use strict";
console.log(this);
return `Hi I am ${this.name} and I can ${thiss}, ${that}, ${thing}, and ${other}. I cannot ${skill}`;
}
console.log(doStuff.apply(person, skills));
console.log(doStuff.call(person, skills[1], skills[0], skills[3], skills[2]));
const personStr = JSON.stringify(person);
console.log(personStr);
const personObj = JSON.parse(personStr);
console.log(personObj);
const personDoes = doStuff.bind(person, skills[1], skills[0], skills[3], skills[2]);
console.log(personDoes());