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
105 changes: 102 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/
function GameObject(objectAttrs) {
this.createdAt = new Date();
this.name = objectAttrs.name;
this.dimensions = objectAttrs.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
*/
function CharacterStats(charAttrs) {
GameObject.call(this, charAttrs);
this.healthPoints = charAttrs.healthPoints;
}

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

CharacterStats.prototype.takeDamage = function() {
if (this.healthPoints <= 0) {
return this.destroy();
}
return `${this.name} took damage and has ${this.healthPoints} hp remaining.`;
};

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
Expand All @@ -32,6 +54,46 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(humanoidAttrs) {
CharacterStats.call(this, humanoidAttrs);
this.team = humanoidAttrs.team;
this.weapons = humanoidAttrs.weapons;
this.language = humanoidAttrs.language;
}

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

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

function Villain(villainAttrs) {
Humanoid.call(this, villainAttrs);
this.vehicle = villainAttrs.vehicle;
}

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

Villain.prototype.punch = function() {
this.healthPoints += 2;
hero.healthPoints -= 3;
console.log(`${this.name} punched ${hero.name} and now has ${this.healthPoints} hp!`);
return hero.takeDamage();
};

function Hero(heroAttrs) {
Humanoid.call(this, heroAttrs);
this.food = heroAttrs.food;
}

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

Hero.prototype.slash = function() {
this.healthPoints += 5;
villain.healthPoints -= 5;
console.log(`${this.name} slashed ${villain.name} and now has ${this.healthPoints} hp!`);
return villain.takeDamage();
};

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
Expand All @@ -41,7 +103,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,6 +154,36 @@
language: 'Elvish',
});

const hero = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 3,
height: 5,
},
healthPoints: 15,
name: 'Al',
team: 'CAPS',
weapon: 'Sword',
language: 'Common Tongue',
food: 'cookies',
});

const villain = new Villain({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 5,
},
healthPoints: 10,
name: 'Kate',
team: 'Strategy',
weapon: 'Hammer',
language: 'Rant',
vehicle: 'pickup',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
Expand All @@ -102,9 +194,16 @@
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(villain.punch()); // Kate punched Al and now has 12 hp! Al took damage and has 12 hp remaining.
console.log(hero.slash()); // Al slashed Kate and now has 17 hp! Kate took damage and has 7 hp remaining.
console.log(hero.slash()); // Al slashed Kate and now has 22 hp! Kate took damage and has 2 hp remaining.
console.log(villain.punch()); // Kate punched Al and now has 4 hp! Al took damage and has 19 hp remaining.
console.log(hero.slash()); // Al slashed Kate and now has 24 hp! Kate 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!

48 changes: 39 additions & 9 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
/* 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 (Console) Object Binding:
Whenever a function is contained in the global scope, the value of "this" inside of the function is the window/console object because functions in the global scope are methods on the window/console object.
* 2. Implicit Binding:
Whenever a function is called by a preceding dot, the value of "this" is the object before the dot.
* 3. New Binding:
Whenever a constructor function is used, the value of "this" is the specific instance of the object that is created and returned by the constructor function.
* 4. Explicit Binding:
It's possible to override constructor functions/objects and explicitly define the value of "this" using the .call or .apply methods.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function sayHi(name) {
console.log(this);
return name;
}
sayHi('Mom');

// Principle 2

// code example for Implicit Binding
const myObj = {
greeting: 'Good evening',
welcomeMessage: function(place) {
console.log(`${this.greeting}, welcome to ${place}.`);
console.log(this);
}
};
myObj.welcomeMessage('Union Square Cafe');

// Principle 3

// code example for New Binding
function Pet(gender, name) {
this.praise = 'Good ';
this.gender = gender;
this.name = name;
this.sit = function() {
console.log(this.praise + this.gender + ', ' + this.name + '!');
console.log(this);
};
}

const peggy = new Pet('girl', 'Peggy');
const steve = new Pet('boy', 'Steve');

peggy.sit();
steve.sit();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
peggy.sit.call(steve);
steve.sit.apply(peggy);