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
101 changes: 96 additions & 5 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
* dimensions
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/
function GameObject(gameObjectAtts) {
this.createdAt = gameObjectAtts.createdAt;
this.dimensions = gameObjectAtts.dimensions;
}
GameObject.prototype.destroy = function() {
return `${this.name} was removed from the game.`;
}

/*
=== CharacterStats ===
Expand All @@ -22,6 +29,15 @@
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(characterStatsAtts) {
GameObject.call(this, characterStatsAtts);
this.hp = characterStatsAtts.hp;
this.name = characterStatsAtts.name;
}
CharacterStats.prototype = Object.create(GameObject.prototype);
CharacterStats.prototype.takeDamage = function() {
return `${this.name} took damage.`;
}

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

function Humanoid(humanoidAtts) {
CharacterStats.call(this, humanoidAtts);
this.faction = humanoidAtts.faction;
this.weapons = humanoidAtts.weapons;
this.language = humanoidAtts.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,7 +67,6 @@

// 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 +127,75 @@
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("\nSTRETCH TASKS");
// Stretch task:
// * Create Villian and Hero constructor functions that inherit from the Humanoid constructor function.
// * Create Villian and Hero constructor functions that inherit from the Humanoid constructor function.
function Villain(villainAtts) {
Humanoid.call(this, villainAtts);
}

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

// * 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!
Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.takeDamage = function() {
this.hp -= 3;
if (this.hp <= 0) {
return `${this.name} took 3pts of damage! ${this.name} has no more hp left! ${this.destroy()}`;
}
return `${this.name} took 3pts of damage! ${this.name}'s hp is now ${this.hp}.`;
}

Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.takeDamage = function() {
this.hp -= 3;
if (this.hp <= 0) {
return `${this.name} took 3pts of damage! ${this.name} has no more hp left! ${this.destroy()}`;
}
return `${this.name} took 3pts of damage! ${this.name}'s hp is now ${this.hp}.`;
}

// * Create two new objects, one a villian and one a hero and fight it out with methods!
const goodGuy = new Hero({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 3,
},
hp: 10,
name: 'Good guy',
faction: 'Earth',
weapons: [
'Sword',
'Gun',
],
language: 'Good guy-ish',
});

const badGuy = new Villain({
createdAt: new Date(),
dimensions: {
length: 3,
width: 2,
height: 1,
},
hp: 8,
name: 'Bad guy',
faction: 'Bad Earth',
weapons: [
'Knife',
'Bow',
],
language: 'Bad guy-ish',
});

console.log(badGuy.takeDamage());
console.log(goodGuy.takeDamage());
console.log(badGuy.takeDamage());
console.log(goodGuy.takeDamage());
console.log(badGuy.takeDamage());
65 changes: 60 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,81 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. When in global scope, the "this" keyword will reference the window or console you are in.
* 2. In impicit binding, the "this" keyword wil reference the object before the dot upon which the method is invoked.
* 3. When creating a new object via a constructor function, the "this" keyword will reference the specific object that is created.
* 4. When you are using the methods call or apply, you are using explicit binding. So when an object calls on another object, the "this" keyword will reference the calling object making the call.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function principle1() {
return this;
}
console.log(principle1());

// Principle 2

// code example for Implicit Binding
const principle2 = {
"greeting": "Hello",
"sayHello": function(myName) {
return `${this.greeting}, my name is ${myName}.`;
}
};
console.log(principle2.sayHello("Alice"));

// Principle 3

// code example for New Binding
function Person(attributes) { // base constructor function
this.greeting = attributes.myGreeting;
this.name = attributes.myName;
}
Person.prototype.sayHello = function() { // methods
return `${this.greeting}, I am ${this.name}.`
};

const bob = new Person({
myGreeting: "Hello",
myName: "Bob"
});
const charles = new Person({
myGreeting: "Hi",
myName: "Charles"
});

console.log(bob.sayHello());
console.log(charles.sayHello());

// Principle 4

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

function Child(childAttributes) {
Parent.call(this, childAttributes);
this.age = childAttributes.age;
}

Child.prototype.speak = function() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
};

const david = new Child({
"name": "David",
"age": 18
});

const ellen = new Child({
"name": "Ellen",
"age": 30
});

console.log(david.speak());
console.log(ellen.speak());